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.
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:
select d from Document d WHERE d.user = :user
AND NOT EXISTS( SELECT b.docs from Book b WHERE b.docs = d )
Where Book is an entity, User is an entity passed in, b.docs is a list of Documents, and Document(d) is an entity.
What am I doing wrong? With this particular version of the query, I’m receiving the error:
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]
Book:
@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;
}
}
UPDATE:
Another try:
Query:
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 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)]
UPDATE2:
I figured this out. I was going about the query all wrong. Solution query was:
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 )
I figured this out. I was going about the query all wrong. Solution query was: