-->
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.  [ 2 posts ] 
Author Message
 Post subject: How to code variable number of 'Or' clauses with Criteria ?
PostPosted: Fri Mar 01, 2013 10:18 am 
Newbie

Joined: Fri Mar 01, 2013 10:12 am
Posts: 1
How to code variable number of 'Or' clauses with Criteria ?

Hi

I have the following criteria query as :

Code:
          Criteria criteria = session.createCriteria(Parts.class);
          Iterator<String> iterator = searchList.iterator();
          Disjunction or =  Restrictions.disjunction();
          while (iterator.hasNext()){
             or.add(Restrictions.like("partName", "%"+iterator.next()+"%"));
          }
          criteria.add(or);
          criteria.add(Restrictions.in("partType", partType));
          criteria.addOrder(Order.asc("partDesc"));

which produces the following where clause :
Code:
   from PartsDb..Parts this_ where (this_.PartName like ? or this_.PartName like ? or this_.PartName like ?) and this_.PartType in (?, ?) order by this_.PartDesc asc

The entire 'OR' clause is in the braces which does not give the correct results. I want the query to be something like this (without the braces around the 'OR' clause):
Code:
   from PartsDb..Parts this_ where this_.PartName like ? or this_.PartName like ? and this_.PartType in (?, ?) order by this_.PartDesc asc

So I am trying to write this :
Code:
   Select * from dbo.Parts
   where
   PartName like '%ABC%'
   or PartName like '%XYZ%'
   or PartName like '%PQR%'
   ....
   ...
   
   and PartType in ('a','b')
   ...

The number of objects in the 'searchList' will vary depending on what the user has entered. Any help on how to get the variable number of 'OR' clauses out of the braces will be highly appreciated !!

Thanks


Top
 Profile  
 
 Post subject: Re: How to code variable number of 'Or' clauses with Criteria ?
PostPosted: Tue Mar 05, 2013 6:48 am 
Senior
Senior

Joined: Tue Oct 28, 2008 10:39 am
Posts: 196
Why do you want it that way?

This http://stackoverflow.com/questions/1241142/sql-logic-operator-precedence-and-and-or should show, that what you want to archive is false as well.

As shown in the article above, your statement would be interpreted as
"from PartsDb..Parts this_ where this_.PartName like ? or (this_.PartName like ? and this_.PartType in (?, ?)) order by this_.PartDesc asc"

Let's assume the users enters the following partNames %ABC%, %DEF% and %GHI% and partTypes 1 and 2.

The correct statement should be (as I see it!)

(partName like %ABC% or partName like %DEF% or partName like %GHI%) and (partType =1 or partType =2)

which may result in a result like the following

ABC123 1
ABC124 2
DEF876 2
GHI653 1

Your desired statement
partName like %ABC% or partName like %DEF% or (partName like %GHI% and (partType =1 or partType =2))

could return the same rows but additionaly
ABC982 6
DEF1234 9

(If 6 and 9 are valid values for partType!)

And the results would be different if the user entered

%ABC%, %GHI% and %DEF%

instead of

%ABC%, %DEF% and %GHI%.

In that case the line "DEF1234 9" cannot be a result row.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 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.