Hibernate对集合的限制

时间:2014-11-11 07:52:16

标签: hibernate

我试图对Book类的字段使用限制,这是一个java.util.Set。 Book类如下:

@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;
    }

}

我在上面的课程中删除了一些字段及其Getters和Setter。 在持久层中,检索书籍的代码如下:

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();

现在我的作者是一个Set和Author类,有字段authorName,authorId。我想对这个authorName加以限制。类别也是如此。 那么怎么做呢?此外,当我不使用投影时,上述限制工作正常。那还有什么原因呢?提前谢谢。

1 个答案:

答案 0 :(得分:0)

我不确定它是否是您问题的根源。但是,您应该为条件添加别名,例如:

Criteria criteria = session.createCriteria(Book.class)
                    .createAlias("authors", "authors")...

类别也应该别名