Hibernate Criteria 存在时无法解析 属性

Hibernate Criteria could not resolve property when it's there

这个让我难住了。我知道标准是如何工作的,但出于某种原因,这个查询给我带来了无法找到明显存在的 属性 的问题。

Shelf.java

@Entity
@Table(name="BOOKSHELF")
public class Shelf {

@Id
private int shelfId;
private String shelfName;
private String shelfType;
@OneToMany(mappedBy="shelf",cascade=CascadeType.ALL)
private Set<Book> book = new HashSet<Book>(0);

//getters&setters here

Book.java

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

@Id
private int bookId;
private String bookName;
private String bookState;
@ManyToOne(cascade=CascadeType.ALL)
private Shelf shelf;    

//getters&setters here

这是我的条件查询:

Criteria criteria = session.createCriteria(Book.class)
                    .add(Restrictions.eq("shelf.shelfId", bookLocId))
                    .add(Restrictions.ne("shelf.shelfType", "table"))
                    .add(Restrictions.ne("bookState", "requested");

问题是中间限制。它无法找到 shelfType,但可以找到 shelfId。感谢您的帮助!

它发现 shelfId 很好,因为它是关联的键,因此查询查看 ID 不需要连接。如果您想对其他 属性 添加限制,您必须加入 shelf 协会,例如:

Criteria criteria = session.createCriteria(Book.class)
         // JOINs shelf association and creates alias 'shelf' for it
         .createAlias("shelf", "shelf")
         .add(Restrictions.eq("shelf.shelfId", bookLocId))
         .add(Restrictions.ne("shelf.shelfType", "table"))
         .add(Restrictions.ne("bookState", "requested");