Hi, I have following problem:
I have two tables: table of calls and table of attributes. Those two tables are in many to many relation. For example:
calls: 'call1', 'call2'
attributes: 'category1', 'category2', 'priority1', 'priority2'
Lets assume that call 'call1' has two attributes: 'category1' and 'priority1'.
Call 'call2' has also two attributes: 'category1' and 'priority2'
Using criteria api I'm trying to find calls which have only and exactly two specified attributes: 'category1' and 'priority1'.
My two approaches are (pCriteria is criteria for call):
First approach:
if (!CollectionUtils.isEmpty(pCallSearchCriteria.getCallAttributes())) {
int count = 0;
Criterion cr = null;
for (Attribute attr : pCallSearchCriteria.getCallAttributes()) {
cr = count++ > 0 ? Restrictions.eq("id", attr.getId()): Restrictions.and(cr, Restrictions.eq("id", attr.getId()));
}
pCriteria.createCriteria("attributes").add(cr);
}
Second approach:
if (!CollectionUtils.isEmpty(pCallSearchCriteria.getCallAttributes())) {
Conjunction attrConj = Restrictions.conjunction();
for (Attribute attr : pCallSearchCriteria.getCallAttributes()) {
attrConj.add(Restrictions.eq("id", attr.getId()));
}
pCriteria.createCriteria("attributes").add(attrConj);
}
Unfortunately it does not work, search results are wrong :-( Please help :-) :-)
|