0

我有用户实体维护一个代表用户朋友的自引用多对多关系。此外,我有一个实体,比如 Attribute,它与 User 实体持有 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
    */
 }
4

0 回答 0