如何将 Hashmap 传递给 spring 数据 jpa 方法并在更新语句中使用它?
How to pass Hashmap into spring data jpa method and use that in update statement?
我想在更新查询中使用 Case 子句更新 table 的多行。
我有一个 Map<String, String>
,其中包含 2 列的值。键作为行的标识符,映射中的值是我要更新的列的值。
如何在 spring 数据 JPA @Query
中执行此操作?
我想实现类似的目标
@Modifying
@Query("update RequestDetail rd set value = VALUE(:statusDetails) where name='Status' and RequestUuid=KEY(:statusDetails)")
void updateBulkStatus(Map<String, String> statusDetails);
但这给出了例外 - antlr.SemanticException: node did not reference a map
。
目标是避免对数据库进行多次更新查询。
我们有什么更好的方法可以将具有多个值的多行更新为单个列。
您需要执行如下操作:
Update RequestDetail rd set rd.value = CASE WHEN (rd.name = :name1) THEN :value1 WHEN (rd.name = :name2) THEN :value2 WHEN (rd.name = :name3) THEN :value3 END
但您不一定知道地图中有多少项目,因此您需要生成查询文本,例如
String sql = "Update RequestDetail rd set CASE ";
int index = 1;
for (Map.Entry<String,String> entry : statusDetails.entrySet()) {
sql += " WHEN (rd.name = :name" + index + ") THEN :value" + (index++) + " ";
}
sql += " END";
您还需要将参数传递给您的查询。
我想在更新查询中使用 Case 子句更新 table 的多行。
我有一个 Map<String, String>
,其中包含 2 列的值。键作为行的标识符,映射中的值是我要更新的列的值。
如何在 spring 数据 JPA @Query
中执行此操作?
我想实现类似的目标
@Modifying
@Query("update RequestDetail rd set value = VALUE(:statusDetails) where name='Status' and RequestUuid=KEY(:statusDetails)")
void updateBulkStatus(Map<String, String> statusDetails);
但这给出了例外 - antlr.SemanticException: node did not reference a map
。
目标是避免对数据库进行多次更新查询。
我们有什么更好的方法可以将具有多个值的多行更新为单个列。
您需要执行如下操作:
Update RequestDetail rd set rd.value = CASE WHEN (rd.name = :name1) THEN :value1 WHEN (rd.name = :name2) THEN :value2 WHEN (rd.name = :name3) THEN :value3 END
但您不一定知道地图中有多少项目,因此您需要生成查询文本,例如
String sql = "Update RequestDetail rd set CASE ";
int index = 1;
for (Map.Entry<String,String> entry : statusDetails.entrySet()) {
sql += " WHEN (rd.name = :name" + index + ") THEN :value" + (index++) + " ";
}
sql += " END";
您还需要将参数传递给您的查询。