开发者

Hibernate query with to join user with friends where user-friends are mapped using many-to-many relationship

I'm using Hibernate and JPA.

public class UserBean {
    private String email;
    private String name;
    private Set<UserBean> friends;
    private Set<UserBean> befriended;
}

@ManyToMany(
    targetEntity=com.pixping.model.UserBean.class,
    fetch = FetchType.LAZY
)
@JoinTable(
    name="user_friends",
    joinColumns=@JoinColumn(name="USER_ID", nullable=false),
    inverseJoinColumns=@JoinColumn(name="FRIEND_ID", nullable=false)
)
public Set<UserBean> getFriends() {
    return friends;
}

public void setFriends(Set<UserBean> friends) {
     this.friends = friends;
}

@ManyToMany(mappedBy = "friends", fetch = Fe开发者_运维技巧tchType.LAZY)
public Set<UserBean> getBefriended() {
    return befriended;
}

I would like to implement this function

public UserBean findUser(Long userId, Long friendId) {

}

where the input is a user id and a friend id and if the user is friends with that person, return the User with the friend, else return just the user. This friend could be in either of the collections 'friends' or 'befriended' and when returned, these collections should contain only this friend and no other friends.

How do I implement a query to do this? If possible please provide an example.


I'm not sure if this can be done using a query (HQL or Criteria), but this can be done using a filter. Have you tried using one?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜