Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use a dto as parameter in a native jpa query

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?

like image 736
anstue Avatar asked Jan 29 '26 11:01

anstue


1 Answers

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)
like image 151
Lookslikeitsnot Avatar answered Jan 31 '26 02:01

Lookslikeitsnot



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!