-->
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.  [ 8 posts ] 
Author Message
 Post subject: Possible to do an OR with Criteria?
PostPosted: Wed Sep 21, 2005 12:17 am 
Beginner
Beginner

Joined: Wed Sep 21, 2005 12:02 am
Posts: 31
I looked in the documentation, but could not find an answer to this question:

I want to be able to allow a user to indicate which fields to be searched and then to search all of them and return any record where one of the fields contains the search term. So I thought of using Criteria, though I am little lost how I would use to do an OR between three values.

For example imagine my conditions are as follow:

Expression.like("title",term);
Expression.like("description",term);
Expression.like("email",term);

I that I need to use Criterion, but that's as much as I understand at the moment.

Any help would be appreciated. Examples even more.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 21, 2005 2:53 am 
Newbie

Joined: Wed Sep 14, 2005 1:31 am
Posts: 6
Do you want to use only Criteria for your requirement. Your search conditions can be done using the HQL:

from obj where
obj.title=:title or obj.description=:description or obj.email=:email


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 21, 2005 3:32 am 
Pro
Pro

Joined: Fri Sep 02, 2005 4:21 am
Posts: 206
Location: Vienna
How about Expression.or(Criterion lhs, Criterion rhs) ?

I'm sorry I can't provide examples, but it seems pretty straightforward.

Erik


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 21, 2005 5:08 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
Criteria OR is done via Restrictions.or(left,right) and/or Restrictions.disjunction()

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 21, 2005 12:52 pm 
Beginner
Beginner

Joined: Wed Sep 21, 2005 12:02 am
Posts: 31
So far I have some code that does not really do what I want it to do:

Criteria criteria = session.createCriteria(Entry.class)
.add(
Expression.or(
Expression.or(Expression.like("title","%"+term+"%"),Expression.like("description","%"+term+"%"))
,Expression.like("comment","%"+term+"%")));

What I would like to do is something like:

Criteria criteria = session.createCriteria(Entry.class);
if ( searchTitle ) { criteria.addOr(Expression.like("title","%"+term+"%")) }
if ( searchDescription ) { criteria.addOr(Expression.like("description","%"+term+"%")) }
if ( searchComment ) { criteria.addOr(Expression.like("comment","%"+term+"%")) }

I suppose I could create the HQL myself, but I was hoping for something that looked a bit cleaner, where I wouldn't have to check whether a previous condition had already been added.

Is this possible? I am looking into this myself, but if someone could save me their time with their knowledge then it would be appreciated.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 21, 2005 2:30 pm 
Beginner
Beginner

Joined: Wed Sep 21, 2005 12:02 am
Posts: 31
I found my answer in the form of the Disjunction class. Below is an example for anyone else needing to the same as me:

Criteria criteria = session.createCriteria(Entry.class);
Disjunction any = Expression.disjunction();
any.add(Expression.like("title","%"+term+"%"));
any.add(Expression.like("description","%"+term+"%"));
any.add(Expression.like("comment","%"+term+"%"));
List l = criteria.list();


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 21, 2005 3:59 pm 
Newbie

Joined: Wed Sep 21, 2005 3:50 pm
Posts: 1
You can also clean it up further with MatchMode.ANYWHERE, i.e.

any.add(Expression.like("title", term, MatchMode.ANYWHERE));

Chris.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 28, 2005 9:26 am 
Newbie

Joined: Wed Sep 14, 2005 1:31 am
Posts: 6
Using Disjunction provides a cleaner solution.
Is there some similar ways to count the number of rows without actually getting the results (using Criteria).
Because if we use HQL, and the number of search conditions are more, checking for a previous condition would result in complex code.


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