-->
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: Variable arguments search
PostPosted: Fri Jul 11, 2008 8:41 am 
Newbie

Joined: Fri Jul 11, 2008 8:17 am
Posts: 1
Hi! I'm new to hibernate.
There is no possibility to have variable number of arguments in JDBC prepared statement, so I would like to ask you how does it look in Hibernate. Let's say I have object with 3 properties:

Code:
class Book {
    String title;
    Double price;
    Date launchDate;
}


and form searching a particular book with 3 input fields for search criteria.
Is there an easy way to search book? I mean searching with 1, 2 or 3 parameters at once (if parameter is not specified it is ignored).


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 11, 2008 1:00 pm 
Beginner
Beginner

Joined: Fri May 14, 2004 9:50 am
Posts: 28
You can use the Criteria API and do it programmatically.

http://www.hibernate.org/hib_docs/refer ... g-criteria


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jul 14, 2008 10:45 am 
Expert
Expert

Joined: Tue May 13, 2008 3:42 pm
Posts: 919
Location: Toronto & Ajax Ontario www.hibernatemadeeasy.com
Indeed, the hibernateCriteria API is a life saver for these types of tasks.

Look at how easy this code is to read and understand. You could easily play around with it and get it to work with your persistence and query scenario.

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());
  }
}


Here's the full Hibernate3 Criteria API tutorial that deals with this Java example:

http://www.hiberbook.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.  [ 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.