I have the following query in JPA, where I want to pass the EntityKey instead of multiple parameters to the method and use it in the where clause:
@Query(value = "UPDATE #{#entityName} SET counter=counter+1 " +
"WHERE id1=:key.id1 AND id2=:key.id2", nativeQuery = true)
@Modifying
fun incrementCounter(@Param("key") key: EntityKey)
Unfortunately the code above doesn't work and I get a Named parameter not bound : key.id1 exception.
Is it possible to pass and use a Dto in the query instead of multiple parameters?
With SpEL, you can access nested parameter attributes with :#{#param.attribute} resulting in :
@Query(value = "UPDATE #{#entityName} SET counter=counter+1 " +
"WHERE id1=:#{#key.id1} AND id2=:#{#key.id2}")
@Modifying
fun incrementCounter(@Param("key") key: EntityKey)
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