我在 JPA 中有以下查詢,我想將EntityKey而不是多個引數傳遞給方法并在 where 子句中使用它:
@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)
不幸的是,上面的代碼不起作用,我得到了一個Named parameter not bound : key.id1例外。是否可以在查詢中傳遞和使用 Dto 而不是多個引數?
uj5u.com熱心網友回復:
一種選擇是使用非本機查詢:
@Query(value = "UPDATE MyEntity e SET e.counter=e.counter 1 "
"WHERE e.id=:key")
@Modifying()
fun incrementCounter(@Param("key") key: EntityKey)
uj5u.com熱心網友回復:
使用 SpEL,您可以訪問嵌套引數屬性,:#{#param.attribute}結果為:
@Query(value = "UPDATE #{#entityName} SET counter=counter 1 "
"WHERE id1=:#{#key.id1} AND id2=:#{#key.id2}")
@Modifying
fun incrementCounter(@Param("key") key: EntityKey)
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/334067.html
