Lets say I have a Book object with a many-to-many association of Authors.
I want to find all Books that have *both* Author A *and* Author B in their Set<Author>.
I tried a criteria search using Restrictions.conjunction, but it results in SQL that tests each Author for both =A and =B, resulting in no matches.
Code:
public List<Book> findByAuthors(Collection<Author> authors) {
DetachedCriteria criteria = DetachedCriteria.forClass(persistentClass);
DetachedCriteria authCriteria = criteria.createCriteria("authors");
authCriteria.add(Restrictions.conjunction());
for (Author author : authors) {
authCriteria.add(Restrictions.eq("name", author.getName()));
}
return findByCriteria(criteria);
}
Is there a way to code this so I will get Books with at least these two Authors, A and B?
Advice appreciated!
-- David