-->
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.  [ 1 post ] 
Author Message
 Post subject: HQL Search
PostPosted: Tue Jul 31, 2007 11:42 am 
Newbie

Joined: Thu Jul 12, 2007 8:57 am
Posts: 11
Is there a way to run an HQL query or Hibernate criteria query that would allow me to search for a string in every persisted attribute of a class?

For example, imagine I have a class Project
This class has attributes name, location, description.

An HQL query string like this
Code:
String hqlString = "FROM Project as p where p.description like '%SEARCH_TERM%'";

will return all of the objects which have their description attribute like the search term. What if I want objects which have that search term in ANY attribute, without specifying which?

I started looking into Hibernate Criteria API to do something like this, but there still doesn't seem to be a way. This leads me to my second question. Adding restriction as below will limit the results, can I instead simply add terms to search for in addition to the others?
I know I can call Restrictions.or, but this only accepts two criterion as arguments.

Code:
String qry = "SEARCH_TERM";
Session session = HibernateHelper.openSession();      
Criteria crit = session.createCriteria(Project.class);
      
crit.add( Restrictions.like("description", "%SEARCH_TERM%") );
crit.add( Restrictions.like("location", "%SEARCH_TERM%") );
      
List results = crit.list();

The above will only return objects whose description AND location matches the search query, I'd like both!

Thanks a lot for your time


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

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.