Hello,
the last weeks I tried to build a Criteria that queries many-to-many associations, without success.
I have USERs, ROLEs and USER_ROLEs where USER_ROLE is the jointable. I want to build a Criteria that gets me all USERs containing a number of ROLEs at the same time (for example 2 or more).
The following attempt does not work properly, because it gets me all USERs that have EITHER Role1 OR Role2. I want them to have BOTH. A Conjunction in place of a Disjunction does not work due to Hibernate creates an SQL like "... where (role_id=1 AND role_id=2)".
Code:
Criteria crit = session.createCriteria(User.class);
Criteria subcrit = crit.createCriteria("roles", CriteriaImpl.INNER_JOIN);
Disjunction any = Expression.disjunction();
any.add( Restrictions.ideq(roleone.getId));
any.add( Restrictions.ideq(roletwo.getId));
subcrit.add(any);
crit.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY) return crit.list();
Can anybody help me with this please? I am frustrated because I struggled with that some days already.