我有一个表user_activities
,其中有user_id
,activity_id
和comments
列。 activity_id
和user_id
都是外键,活动表具有另一个属性activity_name
。要获取具有特定user_id
和activity_name
的行,我可以使用:
FROM UserActivityMapping AS mapping
INNER JOIN mapping.activityId AS activity
WHERE userId = <something> AND activity.activityName = <something>
到目前为止,这么好。现在,给定Pair
的{{1}}列表,我想删除所有这些行。这个列表可能很大..比如600-700项大。现在看起来我可以生成一个查询的大问题:
<UserId, ActivityName>
这显然不是最好的方法。我该怎么做?这样的事情会很棒:
DELETE ...
WHERE (userId = userId[0] AND activity.activityName = activityName[0]) OR
(userId = userId[1] AND activity.activityName = activityName[1]) OR
...
非常感谢任何帮助。
答案 0 :(得分:0)
之前我遇到过类似的问题并写了一个相当简单的函数来动态构建hql
private String buildInClause(List<Integer> keys) {
String inClause = "object.id in (";
for (Integer key : keys) {
inClause += key + ',";
}
inClause = inClause.substring(0,inClause.length -1) + ")"; // strip off last comma and add a closing paren.
return inClause;
}
对于您的问题,您可以稍微修改此方法
inClause = " userId || '-' || activity.activityName in (";
// loop over your pairs however they are passed in
inClause += "'" + userId + "-" + activityName + "',";
// strip off trailing coman and add closing paren
inClause = inClause.substring(0,inClause.length -1) + ")";
// end loop