-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: Many to many criteria query problem
PostPosted: Tue Jul 01, 2008 4:39 am 
Newbie

Joined: Tue Jul 01, 2008 4:20 am
Posts: 1
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 :-) :-)


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.