我需要确定实体是否已被持久化。不幸的是,我没有id,但是如果实体的其他六个字段的值与持久化实体匹配,我可以确定该实体已经被持久化。我正在使用Spring JPA存储库并且知道我可以执行以下操作:
Test findByField1AndField2And...(String field1, String field2,...)
有没有办法做类似的事情:
@Query("SELECT t "
+ "FROM Test t "
+ "WHERE "
+ "t.field1 = :testWithSomeFieldsPopulated.field1 and "
+ "t.field2 = :testWithSomeFieldsPopulated.field2 and ..." )
Test findByTest(@Param("testWithSomeFieldsPopulated") Test testWithSomeFieldsPopulated)
答案 0 :(得分:6)
如果您使用的是Spring Data JPA 1.7.0或更高版本,则可以通过using SpEL in your @Query
definition完成此操作。如下所示:
@Query("SELECT t "
+ "FROM Test t "
+ "WHERE "
+ "t.field1 = :#{#testWithSomeFieldsPopulated.field1} and "
+ "t.field2 = :#{#testWithSomeFieldsPopulated.field2} and ..." )
Test findByTest(@Param("testWithSomeFieldsPopulated") Test testWithSomeFieldsPopulated)