2013-02-09 77 views
4

我有一個表user_activities列,user_id,activity_idcommentsactivity_iduser_id都是外鍵,活動表具有另一個屬性activity_name。要讀取行,與特定user_idactivity_name我可以使用:多個屬性的休眠IN子句

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

到目前爲止,一切都很好。現在,給出<UserId, ActivityName>Pair的列表,我想刪除所有這樣的行。這份名單可能相當大,比如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>>() 
); 

任何幫助,非常感謝。

+2

[言論]的問題,如[/ rant] – biziclop 2013-02-09 11:30:23

+0

@biziclop::(不是我正在尋找的... – 2013-02-09 11:56:40

+0

請檢查這個例子,在這個映射是在兩個不同的包類之間完成的..它可以幫助你http ://www.roseindia.net/hibernate/hibernate4/hibernate_x ml_mapping.shtml – 2013-03-08 06:52:37

回答

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