Hibernate 对集合的休眠限制
我试图对Book类的一个字段使用一个限制,该字段是java.util.Set。图书类别如下:Hibernate 对集合的休眠限制,hibernate,Hibernate,我试图对Book类的一个字段使用一个限制,该字段是java.util.Set。图书类别如下: @Entity public class Book implements java.io.Serializable { /** The Constant serialVersionUID. */ private static final long serialVersionUID = 1L; /** The book id. */ private int bookId
@Entity
public class Book implements java.io.Serializable {
/** The Constant serialVersionUID. */
private static final long serialVersionUID = 1L;
/** The book id. */
private int bookId;
/** The authors. */
private Set<Author> authors = new HashSet<Author>(0);
/**
* Gets the book id.
*
* @return the book id
*/
@Id
@GeneratedValue
@Column(name = "Book_Id")
public int getBookId() {
return bookId;
}
/**
* Sets the book id.
*
* @param bookId
* the new book id
*/
public void setBookId(int bookId) {
this.bookId = bookId;
}
/**
* Gets the authors.
*
* @return the authors
*/
@JsonManagedReference
@ManyToMany(fetch = FetchType.EAGER)
@JoinTable(name = "Book_Author", joinColumns = { @JoinColumn(name = "Book_Id", nullable = false) }, inverseJoinColumns = { @JoinColumn(name = "Author_Id", nullable = false) })
public Set<Author> getAuthors() {
return authors;
}
/**
* Sets the authors.
*
* @param authors
* the new authors
*/
public void setAuthors(Set<Author> authors) {
this.authors = authors;
}
/*
* (non-Javadoc)
*
* @see java.lang.Object#hashCode()
*/
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + bookId;
return result;
}
/*
* (non-Javadoc)
*
* @see java.lang.Object#equals(java.lang.Object)
*/
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
Book other = (Book) obj;
if (bookId != other.bookId) {
return false;
}
return true;
}
}
Criteria criteria = session.createCriteria(Book.class)
.setProjection( Projections.projectionList()
.add( Projections.property("bookId"), "bookId")
.add( Projections.property("publisherName"), "publisherName")
.add( Projections.property("publishedYear"), "publishedYear")
.add( Projections.property("description"), "description")
.add( Projections.property("title"), "title")
.add( Projections.property("image"), "image")
.add( Projections.property("availability"), "availability")
.add( Projections.property("authors"))
.add( Projections.property("categories"))
).setResultTransformer(Transformers.aliasToBean(Book.class));
criteria.add(
Restrictions.conjunction().add(
Restrictions.like("availability", "AVAILABLE")))
.add(Restrictions
.disjunction()
.add(Restrictions.like("title", bookAttributesLike, MatchMode.ANYWHERE))
.add(Restrictions.like("authors.authorName",
bookAttributesLike, MatchMode.ANYWHERE))
.add(Restrictions.like("categories.categoryName",
bookAttributesLike, MatchMode.ANYWHERE)));
result = criteria.list();
现在I authors是一个集合,Author类具有字段authorName,authord。我想限制这个authorName。类别的情况也类似。
那么如何做到这一点呢?此外,当我不使用投影时,上述限制也可以正常工作。那么原因是什么呢?提前谢谢你。我不确定这是否是你问题的根源。但是,应将别名添加到条件中,例如:
Criteria criteria = session.createCriteria(Book.class)
.createAlias("authors", "authors")...
此外,还应为类别添加别名