The criteria API makes it all real easy. Just use the Restrictions class and you'll find this is very easy to do, substituting real values into the restrictions at runtime.
http://jpa.ezhibernate.com/Javacode/lea ... riteriaapi
Here's a little sample code from my website. As you can see, I create dynamic SQL using the Restrictions class, asking for values that are less than one number, greater than another, while making sure the email address is not null and has an email address that ends in .com. It's all real easy!
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 Tutorial teaching you how to use the Hiberante Criteria API:
http://jpa.ezhibernate.com/Javacode/learn.jsp?tutorial=09howtousethecriteriaapi