-->
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.  [ 3 posts ] 
Author Message
 Post subject: How to apply mulitple or condition using Criteria
PostPosted: Mon May 07, 2007 3:36 am 
Newbie

Joined: Sat Dec 23, 2006 7:14 am
Posts: 2
Hibernate version: 3.2.0

Hi experts,
I have to filter a record for multiple values of a column. I have a table say "TBL" and have a field say "f". Now i need to apply a condition
as given below
Code:
select * from TBL where f like 'x' or f like 'y' or f like 'z';


I think i can do it using the following statement:
Code:
criteria.createCriteria(Tbl.class).add(Restrictions.or(Restrictions.ilike("f","x"),Restrictions.or(Restrictions.ilike("f","y"), Restrictions.ilike("f","z"))));


as far as i know the above statement will produce something like:
Code:
select * from TBL where (f like 'x' or (f like 'y' or f like 'z'));


but my problem is, at runtime i know how many values (x, y, z) need to be checked against the property 'f';

How do i do that using Criteria API

thanks in advance


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 07, 2007 6:52 am 
Regular
Regular

Joined: Mon Mar 26, 2007 12:38 am
Posts: 119
Hi,
Here is Java way of doing it.
Build criteria at run time.

org.hibernate.criterion.Property myProperty = Property.forName("f") ;
Disjunction or = Restrictions.disjunction() ;
for(String value : values)
or.add(myProperty.like(value)) ;
session.createCriteria(Tbl.class).add(or).list() ;


where, values is of type List<String> and holds values for comparison. ( 'x' , 'a' .... )

-------------------------------------------------
Rate the reply if you find it helpful


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 07, 2007 1:35 pm 
Newbie

Joined: Sat Dec 23, 2006 7:14 am
Posts: 2
Thanks

It is working fine.


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