我的用户实体维护一个代表用户朋友的自引用ManyToMany关系。此外,我有一个实体,比如属性,与用户实体持有ManyToOne关系。
现在我想通过一个NamedQuery检索并给予一个用户u,所有用户的朋友都是一个确切的属性值。
查询是什么?
我尝试了类似的东西:
"SELECT a.user "+
"FROM Attribute a "+
"WHERE a.attribute= :attribute AND "+
"c.user IN ( :user.friends )"
这在初始化应用程序时创建sessionFactory时会给我一个空指针异常。
以下是实体:
@Entity
public class User implements Serializable {
private static final long serialVersionUID = 1L;
@Id
private String id;
private String nickname;
private String thumbnailUrl;
@ManyToMany
@JoinTable(
name = "user_friends",
joinColumns = @JoinColumn(name = "user_id"),
inverseJoinColumns = @JoinColumn(name = "friend_id")
)
protected List<User> friends = new ArrayList<User>();
@ManyToMany(mappedBy = "friends")
protected List<User> befriended = new ArrayList<User>();
/*
* getters and setters...
*/
}
public class Attribute implements Serializable {
/*
* other fields...
*/
@ManyToOne
@JoinColumn(name="userId")
private User user;
/*
* getters and setters
*/
}