-->
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.  [ 4 posts ] 
Author Message
 Post subject: Multi query using criteria
PostPosted: Mon May 26, 2008 7:10 am 
Newbie

Joined: Mon May 26, 2008 7:03 am
Posts: 2
I am writing a web app using hibernate java struts. I have a piece of code for a search bar that follows:

List listofsome = session.createCriteria(some.class)
.add(Restrictions.like("firstName", searchString+"%")).list();

this works but only searches for the string on one column of my database.

My question is can i incorporate another line of code into this to query another column? or do i have to do it another way if so how?

all help appreciated

Thank you

Danny


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 26, 2008 9:40 am 
Senior
Senior

Joined: Wed Sep 19, 2007 9:31 pm
Posts: 191
Location: Khuntien (Indonesia)
Code:
Criteria criteria = session.createCriteria(someClass);
Disjunction disj = Restrictions.disjunction();
disj.add(Restrictions.ilike(column1, value, MatchMode.ANYWHERE);
disj.add(Restrictions.ilike(column2, value, MatchMode.ANYWHERE);
criteria.add(disj);
List list = criteria.list();


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 26, 2008 9:57 am 
Newbie

Joined: Mon May 26, 2008 7:03 am
Posts: 2
Thanks very much mate, works a treat.


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 26, 2008 12:46 pm 
Expert
Expert

Joined: Tue May 13, 2008 3:42 pm
Posts: 919
Location: Toronto & Ajax Ontario www.hibernatemadeeasy.com
Certainly you can just keep adding in criteria queries like crazy. That's the beauty of the Hibernate Critieria API.

Code:
public static void main(String args[]) {
  Session session = HibernateUtil.beginTransaction();
  Criterion c1 = Restrictions.gt("id", (long)2);
  Criterion c2 = Restrictions.lt("id", (long)8);
  Criterion c3 = Restrictions.isNotNull("emailAddress");
  User user = new User();
  user.setEmailAddress(".com");
  Example c4 = Example.create(user);
  c4.enableLike(MatchMode.END);
  c4.ignoreCase();
  Criteria criteria = session.createCriteria(User.class);
  criteria.add(c1);
  criteria.add(c2);
  criteria.add(c3);
  criteria.add(c4);
  List results = criteria.list();
  HibernateUtil.commitTransaction();
  for (int i = 0; i<results.size(); i++) {
    System.out.println(results.get(i).toString());
  }
}


The code above uses a disjunction, which provides an OR type of query=. Without using the disjunction, you'll default to the and query.

Here's a friendly little tutorial on how to use the Hibernate Criteria API, along with some Hibernte code snippets and Hibernate examples:

http://www.hibernatemadeeasy.com/HiberBookWeb/learn.jsp?tutorial=09howtousethecriteriaapi

_________________
Cameron McKenzie - Author of "Hibernate Made Easy" and "What is WebSphere?"
http://www.TheBookOnHibernate.com Check out my 'easy to follow' Hibernate & JPA Tutorials


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