Hibernate criteria query to match against all child collection elements

Posted by Andrew Harrison on Stack Overflow See other posts from Stack Overflow or by Andrew Harrison
Published on 2010-05-21T21:29:50Z Indexed on 2010/05/21 21:30 UTC
Read the original article Hit count: 825

Filed under:
|

Hi,

This question is very similar to link text but the responses were minimal to that question.

I have a parent class with a Set of child entities. The child entities are just a wrapper for a string and live in a different table to the parent entity. I want to have a criteria query that returns the parent entities when all the members of the set of child entities return true to a condition. This condition is matching against one of a list of strings. Here's where I am:

Criteria c = criteria();
Criteria ands = c.createCriteria("ands");
Disjunction dis = Restrictions.disjunction();
for (String value : values) {
    dis.add(Restrictions.like("value", "%" + value + "%"));
}
ands.add(dis);
return list(c);

"ands" is the set of entities with a "value" field that is a string. "criteria()" creates a criteria for the parent class. "list()" just calls criteria.list();

This is just matching against any of the elements, rather than all.

Hope this makes sense. Any help much appreciated.

© Stack Overflow or respective owner

Related posts about java

Related posts about hibernate