Young9007
2012-10-15 18:24 阅读 215
已采纳

hibernate多对多自身映射的查询问题

有个User类,User之间可以互相关注,因此User类下面有个多对多的自身映射,关注者和被关注者
[code="java"]
@ManyToMany(fetch = FetchType.LAZY)
@JoinTable(name = "t_user_t_followingUser", joinColumns = { @JoinColumn(name = "user_id") },inverseJoinColumns = { @JoinColumn(name = "followingUser_id") })
public List getFollowingUsers() {
return followingUsers;
}

public void setFollowingUsers(List 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 getFollowedUsers() {
return followedUsers;
}

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

[/code]

现在要写一个HQL语句查询出一个User所有的粉丝。
这个HQL该如何写呢?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

1条回答 默认 最新

  • 已采纳
    jinnianshilongnian jinnianshilongnian 2012-10-15 21:03

    试试
    select fu from Users u left join u.followedUsers fu;

    点赞 评论 复制链接分享

相关推荐