2012-10-15 88 views
0

我有用戶模型,系統允許用戶關注對方。 所以我把下面的用戶模型兩個列表:如何編寫HQL查詢多對多?

@ManyToMany(fetch = FetchType.LAZY) 
@JoinTable(name = "t_user_t_followingUser", joinColumns = { @JoinColumn(name = "user_id") }, inverseJoinColumns = { @JoinColumn(name = "followingUser_id") }) 
public List<User> getFollowingUsers() { 
    return followingUsers; 
} 

public void setFollowingUsers(List<User> followingUsers) { 
    this.followingUsers = followingUsers; 
} 

@ManyToMany(fetch = FetchType.LAZY) 
@JoinTable(name = "t_user_t_followedUser", joinColumns = { @JoinColumn(name = "user_id") }, inverseJoinColumns = { @JoinColumn(name = "followedUser_id") }) 
public List<User> getFollowedUsers() { 
    return followedUsers; 
} 

public void setFollowedUsers(List<User> followedUsers) { 
    this.followedUsers = followedUsers; 
} 

現在我想獲得用戶u的總追隨者。 如何編寫HQL?

ps:many2many可能不適合。並有任何建議的設計?

在此先感謝。

+0

你是怎麼試圖給你寫查詢的? 「 –

+0

」select count(*)from User u left join u.followedUsers where u =?「好像是工作。 –

回答

0

試試這個SELECT COUNT(fu) FROM User AS u INNER JOIN u.followedUsers AS fu;