【问题标题】:JPQL issue resolving nodes of a collection to compare in queryJPQL 问题解析集合的节点以在查询中进行比较
【发布时间】:2012-03-21 12:05:10
【问题描述】:

我有一个书籍实体,它与 Document 实体建立了 OneToMany 关系。换句话说,在 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是文档列表,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 未映射 [从 com.fallenjusticestudios.bardwalk.model.Document d 中选择 d WHERE d.user = :user AND NOT EXISTS( 从 Book.docs 中选择文档b WHERE b.id = dn.id)]

更新2:

我想通了。我正在查询所有错误。解决方案查询是:

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 )

【问题讨论】:

    标签: hibernate jpa jpql


    【解决方案1】:

    我想通了。我正在查询所有错误。解决方案查询是:

    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 )
    

    【讨论】:

      猜你喜欢
      • 2013-11-27
      • 2019-04-20
      • 2014-05-04
      • 2023-01-31
      • 2020-06-11
      • 2020-12-27
      • 2017-01-01
      • 2012-10-31
      • 2015-02-13
      相关资源
      最近更新 更多