我有一個圖書實體,它具有設置爲文檔實體的OneToMany關係。換句話說,在Hibernate中,我的book實體返回一個文檔列表作爲屬性文檔。JPQL問題解決查詢中要比較的集合的節點
我想返回尚未分配給書籍的用戶的文檔列表。這裏是我的JPQL查詢,我已經finagled它的每一個方式,我可以,我不能得到它的工作:
select d from Document d WHERE d.user = :user
AND NOT EXISTS(SELECT b.docs from Book b WHERE b.docs = d)
凡書是一個實體,用戶是通過一個實體,b.docs是文件清單,文件(d)是實體。
我在做什麼錯?與查詢的這個特殊的版本,我收到了錯誤:
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]
書:
@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:
闖闖:
查詢:
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:
我想通了。我正在討論所有錯誤的查詢。解決方案查詢是:
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)