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?
CodePudding user response:
One Option is to use none native queries:
@Query(value = "UPDATE MyEntity e SET e.counter=e.counter 1 "
"WHERE e.id=:key")
@Modifying()
fun incrementCounter(@Param("key") key: EntityKey)
CodePudding user response:
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)