Hibernate criteria restriction on a property for all elements of a set

北城以北 提交于 2019-12-25 01:46:41

问题


I have an entity with a set say like

Library---><Set>books

Now I want to retrieve the libraries where ALL the books have a genre.

So I have something like this:

c.createCriteria("library", "library").createCriteria("books", "book");
c.add(Restrictions.isNotNull("book.genre"));

If I execute the query I get the libraries where at least one book has a genre but I'd like hibernate to check the genre property for all the elements of the book set and return the libraries where ALL the elements satisfy the not null restriction.

Sorry for my English, I hope the problem is clear, any help is very much appreciated.

Thanks.


回答1:


May be something like:

FROM Library library
WHERE not exists (SELECTbook from Book book 
    where book.description=null and book.id in (library.books)) ? 

Just an idea...



来源:https://stackoverflow.com/questions/1888445/hibernate-criteria-restriction-on-a-property-for-all-elements-of-a-set

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!