Note that there are some explanatory texts on larger screens.

plurals
  1. POJPQL issue resolving nodes of a collection to compare in query
    primarykey
    data
    text
    <p>I have a book entity which has a OneToMany relationship set up to Document entity. In other words, in Hibernate, my book entity returns a list of Documents as the property docs.</p> <p>I want to return a list of documents for a user which have not been assigned to a book, yet. Here is my jpql query, and I've finagled it every way I can, and I can't get it to work:</p> <pre><code>select d from Document d WHERE d.user = :user AND NOT EXISTS( SELECT b.docs from Book b WHERE b.docs = d ) </code></pre> <p>Where Book is an entity, User is an entity passed in, b.docs is a list of Documents, and Document(d) is an entity.</p> <p>What am I doing wrong? With this particular version of the query, I'm receiving the error: </p> <blockquote> <p>org.hibernate.TypeMismatchException: left and right hand sides of a binary logic operator were incompatibile [java.util.Collection(com.fallenjusticestudios.bardwalk.model.Book.docs) : com.fallenjusticestudios.bardwalk.model.Document]</p> </blockquote> <p>Book:</p> <pre><code>@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&lt;Document&gt; 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&lt;Document&gt; 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&lt;Document&gt; docs) { this.docs = docs; } } </code></pre> <p>UPDATE: </p> <p>Another try:</p> <p>Query:</p> <pre><code>select d from Document d WHERE d.user = :user AND NOT EXISTS( SELECT Document from Book.docs b WHERE b.id = dn.id ) </code></pre> <blockquote> <p>org.hibernate.hql.ast.QuerySyntaxException: Book.docs is not mapped [select d from com.fallenjusticestudios.bardwalk.model.Document d WHERE d.user = :user AND NOT EXISTS( SELECT Document from Book.docs b WHERE b.id = dn.id)]</p> </blockquote> <p>UPDATE2:</p> <p>I figured this out. I was going about the query all wrong. Solution query was:</p> <pre><code>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 ) </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload