4

我有一个user_activities包含列、user_idactivity_id的表commentsactivity_id并且user_id都是外键,活动表具有另一个属性activity_name。要获取一行,具有特定的user_idactivity_name我可以使用:

FROM UserActivityMapping AS mapping
    INNER JOIN mapping.activityId AS activity
    WHERE userId = <something> AND activity.activityName = <something>

到目前为止,一切都很好。现在,给定一个Pairs 的列表<UserId, ActivityName>,我想删除所有这样的行。这份清单可能非常大……就像 600-700 件物品一样大。现在看起来我可以生成一个非常混乱的查询:

DELETE ...
    WHERE (userId = userId[0] AND activity.activityName = activityName[0]) OR
          (userId = userId[1] AND activity.activityName = activityName[1]) OR
          ...

这显然不是最好的方法。我该怎么做?像这样的事情会很棒:

"WHERE (userId, activityId) in :userActivityList".setParameterList(
    new List<Pair<String, String>>()
);

任何帮助是极大的赞赏。

4

1 回答 1

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
于 2014-03-04T17:49:21.857 回答