0

我有用户模型,系统允许用户互相关注。所以我在 User 模型中放了两个列表如下:</p>

@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 可能不合适。对设计有什么建议吗?

提前致谢。

4

1 回答 1

0

尝试这个SELECT COUNT(fu) FROM User AS u INNER JOIN u.followedUsers AS fu;

于 2012-10-15T13:32:34.750 回答