Hibernate Query Association with Criteria: 'and' on children?
Let's say I have a parent class Book, each with a list of Authors. I am trying to find all the books with at least two Authors one named "Bob" and another one named "Alan".
Here is my Groovy code that doesn't work, it looks for books with an author named "Bob" and "Alan" at the same time, so it returns 0 items. I tried to create 2 aliases to create 2 joins, but Hibernate refuses to create 2 aliases to the same collection.
def crit = hsession.createCriteria(Book.class).createCriteria("authors").add(Restrictions.eq("name", "Bob")).add(Restrictions.eq("name", "Alan"))
你需要像这样使用LogicalExpression
def crit = hsession.createCriteria(Book.class)
.createAlias("authors", "author")
.add(Restrictions.or(Restrictions.eq("author.name", "Bob"), Restrictions.eq("author.name", "Alan")))
链接地址: http://www.djcxy.com/p/36996.html