JPQL问题解析集合中的节点以在查询中进行比较

时间:2012-03-03 01:30:41

标签: hibernate jpa jpql

我有一个图书实体,其OneToMany关系设置为Document实体。换句话说,在Hibernate中,我的book实体返回一个Documents列表作为属性docs。

我想返回尚未分配给图书的用户的文档列表。这是我的jpql查询,我已经尽我所能地完成了它,我无法让它工作:

select d from Document d WHERE d.user = :user
AND NOT EXISTS( SELECT b.docs from Book b WHERE b.docs = d )

Book是实体,User是传入的实体,b.docs是Documents列表,Document(d)是实体。

我做错了什么?使用此特定版本的查询,我收到错误:

  

org.hibernate.TypeMismatchException:二进制逻辑的左侧和右侧   运营商是不兼容的   [java.util.Collection(com.fallenjusticestudios.bardwalk.model.Book.docs):com.fallenjusticestudios.bardwalk.model.Document]

图书:

@Entity
@Table(name="book")
public class Book {

    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    private Long id;

    @Column
    @NotEmpty
    @NotNull
    private String title;

    @Column
    @NotEmpty
    @NotNull
    private String description;

    @ManyToOne(cascade=CascadeType.MERGE, targetEntity=User.class)
    @JoinColumn(name="user")
    private User user;

    @OneToMany(cascade=CascadeType.MERGE, fetch=FetchType.LAZY, targetEntity=Document.class)
    @JoinColumn(name="document_id",referencedColumnName="id")
    private List<Document> docs;


    // Will need to add Contest to the fields later on.
    //
    //


    public Long getId() {
        return id;
    }

    public String getTitle() {
        return title;
    }

    public String getDescription() {
        return description;
    }

    public User getUser() {
        return user;
    }

    public List<Document> getDocs() {
        return docs;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public void setUser(User user) {
        this.user = user;
    }

    public void setDocs(List<Document> docs) {
        this.docs = docs;
    }





}

更新:

另一次尝试:

查询:

select d from Document d WHERE d.user = :user
AND NOT EXISTS( SELECT Document from Book.docs b WHERE b.id = dn.id )
  

org.hibernate.hql.ast.QuerySyntaxException:Book.docs未映射[select d from com.fallenjusticestudios.bardwalk.model.Document d WHERE d.user =:user AND NOT EXISTS(来自Book.docs的SELECT文档) b WHERE b.id = dn.id)]

UPDATE2:

我想出来了。我的查询都错了。解决方案查询是:

select d from Document d WHERE d.user = :user
AND NOT d IN( SELECT d from Book b, IN(b.docs) bd WHERE bd.id = d.id )

1 个答案:

答案 0 :(得分:0)

我想出来了。我的查询都错了。解决方案查询是:

select d from Document d WHERE d.user = :user
AND NOT d IN( SELECT d from Book b, IN(b.docs) bd WHERE bd.id = d.id )