I’m using JPA 2.0 with Hibernate 4.1.0.Final. I have a couple of classes, Groups and GroupMembers. Each GroupMember is tied to a user object
@Entity
@Table
You will need different roots for each and every user, as each of them is different from the others (untested):
final CriteriaBuilder builder = m_entityManager.getCriteriaBuilder();
CriteriaQuery criteria = builder.createQuery(Group.class);
final List predicates = new ArrayList();
final Root group = criteria.from(Group.class);
for (final User user : users)
{
final Root memberRoot = group.join(Group_.members);
final Predicate p = builder.equal(memberRoot.get(GroupMember_.user), user);
predicates.add(p);
} // for
predicates.add(builder.equals(builder.count(group.get(Group_.members)),users.size()))
criteria.where(builder.and(predicates.toArray(new Predicate[predicates.size()])));
final TypedQuery results = m_entityManager.createQuery(criteria);