I need to determine if an entity has already been persisted. Unfortuantely, I do not have the id, but I can determine that the entity is already persisted if the value of six other fields of the entity match a persisted entity. I'm using Spring JPA repositories and know that I can do the following:
Test findByField1AndField2And...(String field1, String field2,...)
Is there a way to do something similar to:
@Query("SELECT t "
+ "FROM Test t "
+ "WHERE "
+ "t.field1 = :testWithSomeFieldsPopulated.field1 and "
+ "t.field2 = :testWithSomeFieldsPopulated.field2 and ..." )
Test findByTest(@Param("testWithSomeFieldsPopulated") Test testWithSomeFieldsPopulated)
If you're using Spring Data JPA 1.7.0 or above, then you can accomplish this by using SpEL in your @Query
definition. So something like:
@Query("SELECT t "
+ "FROM Test t "
+ "WHERE "
+ "t.field1 = :#{#testWithSomeFieldsPopulated.field1} and "
+ "t.field2 = :#{#testWithSomeFieldsPopulated.field2} and ..." )
Test findByTest(@Param("testWithSomeFieldsPopulated") Test testWithSomeFieldsPopulated)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With