-->
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.  [ 7 posts ] 
Author Message
 Post subject: How to create a query with multiple OR/AND conditions
PostPosted: Tue Feb 20, 2007 10:37 pm 
Newbie

Joined: Tue Feb 20, 2007 10:30 pm
Posts: 3
I need to create a query which looks like this:

select * from table where table.column1='something' OR table.column1='something else' OR table.column1='something else else' OR .... to n

i looked at Expression.or but it only lets me specify two criteria, how do i add more?


Any help will be appreciated.

_________________
Gaurav Taneja


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 20, 2007 11:22 pm 
Expert
Expert

Joined: Tue Jan 30, 2007 12:45 am
Posts: 283
Location: India
Hi gauravtaneja,

Try

Restrictions.Or()

or you could use HQL.

_________________
Dharmendra Pandey


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 20, 2007 11:56 pm 
Newbie

Joined: Tue Feb 20, 2007 10:30 pm
Posts: 3
but Restrictions.or(Criterion lhs, Criterion rhs) takes only two arguments. i want to be able to write n numbers of OR statements.

as i said in my original post. select * from table where table.column='foo' OR table.column='foobar' OR table.column='fooblah' and so on....

how do i achieve that?

_________________
Gaurav Taneja


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 21, 2007 12:31 am 
Regular
Regular

Joined: Sun Sep 17, 2006 2:48 am
Posts: 81
Location: California
Restrictions.or() returns Criterion. So you can call Restrictions.or multiple times.

Hope that helps.
Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 21, 2007 3:53 am 
Newbie

Joined: Tue Dec 12, 2006 3:19 am
Posts: 17
Location: Spain
Hi,
As somu said, you can call Restriction.or several times, so you may code something like that:

Code:
// 3 restrictions
criterio.add(Restrictions.or(
   Restrictions.or(Restrictions.eq("column1", "1"),Restrictions.eq("column1", "2")),
   Restrictions.eq("column1", "3")
));

or

Code:
//4 restrinctions
criterio.add(Restrictions.or(
   Restrictions.or(Restrictions.eq("column1", "1"),Restrictions.eq("column1", "2")),
   Restrictions.or(Restrictions.eq("column1", "3"),Restrictions.eq("column1", "4"))
));


Also I think you could try in restriction. If you are going to use the same column (column1).

Code:
select * from table where table.column1 IN ('something', 'something else' , 'something else else'...)

_________________
Please, rate this posting if you find it useful


Last edited by i92absaj on Thu Feb 22, 2007 3:31 am, edited 1 time in total.

Top
 Profile  
 
 Post subject: thanks a ton
PostPosted: Wed Feb 21, 2007 1:59 pm 
Newbie

Joined: Tue Feb 20, 2007 10:30 pm
Posts: 3
Thanks a ton guys. this was really helpful

_________________
Gaurav Taneja


Top
 Profile  
 
 Post subject: Re: How to create a query with multiple OR/AND conditions
PostPosted: Wed Aug 12, 2009 1:40 pm 
Newbie

Joined: Wed Aug 12, 2009 1:34 pm
Posts: 1
What if i need to write a query that matches partial values and returns all the items.

Equivalent sql should be
select * from table where propName like '%value1% or propName like '%value2% or propName like '%value3% or propName like '%value4%

I need to write a Hibernate criteria for the above, how can i do that?


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 7 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:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.