-->
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.  [ 5 posts ] 
Author Message
 Post subject: Need help with Criteria building where clause dynamically
PostPosted: Thu Feb 19, 2009 11:27 am 
Newbie

Joined: Wed Feb 18, 2009 5:19 pm
Posts: 5
I am using the Criteria API and am having trouble building a where clause like the following.

WHERE
(col1 = ? AND col2 IN (?) AND col3 = ?) OR
(col1 = ? AND col2 IN (?) ) AND col3 = ?) OR
(col1 = ? AND col2 IN (?) ) AND col3 = ?)

I need to use “AND” to evaluate 3 columns and “OR” for each line. The number of rows will be built dynamically within a loop.

I’ve tried this.

criteria.add(Restrictions.or(Restrictions.and(
Restrictions.eq(“col1”, col1 ),
Restrictions.eq(“col2”, col2)),
Restrictions.eq(“col3”, col3)));

But the SQL where clause it produced was this:

AND ( (this_.col1 = ? AND this_.col2 = ?) OR this_.col3 = ?

I’ve also tried using the conjunction and disjunction but couldn’t get that working correctly either.

I was able to find examples that show how to evaluate 2 columns but couldn’t get this to work for 3 columns.

disjunction.add(Restrictions.and(
Restrictions.eq(“cole1”, col1 ),
Restrictions.eq(“col2”, col2) ));

Is it possible to extend this example to include 3 restrictions?


Thanks for any help you can offer in solving this.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 19, 2009 11:40 am 
Expert
Expert

Joined: Thu Jan 08, 2009 6:16 am
Posts: 661
Location: Germany
Restrictions.conjunction() and disjunction() let you add as many Criterions as you want, e.q.:
Code:
Criteria c = s.createCriteria(Customer.class);
        Conjunction c1 = Restrictions.conjunction();
        c1.add(Restrictions.eq("someProperty", 1)).add(Restrictions.eq("someProperty2", 2)).add(Restrictions.eq("someProperty2", 2));
        Conjunction c2 = Restrictions.conjunction();
        c2.add(Restrictions.eq("someProperty", 1)).add(Restrictions.eq("someProperty2", 2));
        c.add(Restrictions.disjunction().add(c1).add(c2));
        c.list();

Results in
Quote:
select
this_.id as id1_0_,
this_.someProperty as someProp2_1_0_,
this_.someProperty2 as someProp3_1_0_,
this_.toOne_id as toOne4_1_0_
from
Customer this_
where
(
(
this_.someProperty=?
and this_.someProperty2=?
and this_.someProperty2=?
)
or (
this_.someProperty=?
and this_.someProperty2=?
)
)

Which is what you want.

Rating appreciated. ;-)

_________________
-----------------
Need advanced help? http://www.viada.eu


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 19, 2009 11:40 am 
Expert
Expert

Joined: Thu Jan 08, 2009 6:16 am
Posts: 661
Location: Germany
double post

_________________
-----------------
Need advanced help? http://www.viada.eu


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 19, 2009 3:32 pm 
Newbie

Joined: Wed Feb 18, 2009 5:19 pm
Posts: 5
Thanks for the example but I am still having problems.

I am starting to wonder if this isn't a bug in my version of Hibernate - version 3.2.5



Session session = HibernatePersistenceManager.getInstance()
.getSession();

Criteria c = session.createCriteria(QuoteLinePSPartResalePR.class);

Conjunction c1 = Restrictions.conjunction();



// in my loop add to the criteria

c1.add(Restrictions.eq(quoteCol, quoteId)).add(
Restrictions.eq(lineCol, quoteLineNoId)).add(
Restrictions.eq(requestedQtyCol, requestedQty));
c.add(Restrictions.disjunction().add(c1));

// end loop



// then outside of the loop I execute

c.list();





Produces this SQL where clause:

WHERE (( this_.quote_id = ?
AND this_.quote_line_no = ?
AND this_.requested_qt = ?
)
)
AND (( this_.quote_id = ?
AND this_.quote_line_no = ?
AND this_.requested_qt = ?
)
)
AND (( this_.quote_id = ?
AND this_.quote_line_no = ?
AND this_.requested_qt = ?
)
)

Am I doing something wrong here?

thanks.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 19, 2009 3:56 pm 
Newbie

Joined: Wed Feb 18, 2009 5:19 pm
Posts: 5
I found my mistake.

I needed to add the disjunction to the criteria outside of the loop. Everything looks good now.

Thanks again for your help.


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

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:
cron
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.