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