-->
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: Building Dynamic where cluase with or using criteria query
PostPosted: Fri Jun 20, 2008 12:47 am 
Newbie

Joined: Wed Jun 18, 2008 3:51 am
Posts: 4
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

Hibernate version:
3.2
Mapping documents:

Code between sessionFactory.openSession() and session.close():

Full stack trace of any exception that occurs:

Name and version of the database you are using:
Oracle 9i
The generated SQL (show_sql=true):

Debug level Hibernate log excerpt:

how do i build a dynamic where cluase with or conditions using criteria query.

where (typeid = ? and name = ? ) or (typeid= ? and address = ?) or (typeid = ? and name = ? and address = ?)

Each of the criteria is determined at run time based on the no of types selected.

i have considered building the sql query using string concatenation and add it to criteria as sql restriction.

Please suggest if this is the best way to do it or is there any alternative.

Problems with Session and transaction handling?

Read this: http://hibernate.org/42.html


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 23, 2008 11:07 am 
Expert
Expert

Joined: Tue May 13, 2008 3:42 pm
Posts: 919
Location: Toronto & Ajax Ontario www.hibernatemadeeasy.com
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

_________________
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  
 
 Post subject:
PostPosted: Mon Jun 23, 2008 11:58 pm 
Newbie

Joined: Wed Jun 18, 2008 3:51 am
Posts: 4
hi Cameron,

Thanks for the reply.
In the given example , the query will be build using "and" condition .
i.e c1 and c2 and c3 and c4.
what i was looking at is c1 or c2 or c3 or c4.
We can do it using conjunction and disjunction,
use conjunction for the (typeid= ? and name = ?) and add it to a disjunction.


Last edited by manishoswal on Tue Jun 24, 2008 1:34 am, edited 1 time in total.

Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 24, 2008 12:25 am 
Newbie

Joined: Wed Jun 18, 2008 3:51 am
Posts: 4
Just to add on , we can do it using conjunction and disjunction,
use conjunction for the (typeid= ? and name = ?) and add it to a disjunction.


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.