-->
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.  [ 5 posts ] 
Author Message
 Post subject: When to use HQL , Query by Criteria , Query by example
PostPosted: Fri Apr 28, 2006 2:05 am 
Beginner
Beginner

Joined: Thu Apr 20, 2006 3:44 am
Posts: 32
Can any tell me , any guidelines, in which schenario we should use HQL , Query by Criteria , Query by example?

Basically I am looking for some guidelines or tips for using HQL , Query by Criteria , Query by example while retrieval of data from database.

Sudhir


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 28, 2006 2:24 am 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
Use HQL when you know the exact shape of the query. For example, if you were implementing a method like "findPersistedObjectByDate(Date d);".

Use normal Criteria when you don't know the exact shape of the query. For example, when your query is being built from user-entered values, where some values are optional.

Use query-by-example Criteria as a shortcut for normal Criteria when you have a similar object. It's also useful if your query is simple but you don't have access to hibernate classes in the place you're building the query. For example, in a webapp action class, you mightn't be allowed (because of company policy) to use hibernate DetachedCriteria. So you could build an object with the user-entered search criteria, and pass that down through your business logic to your data access layer, and that does the search by example.

_________________
Code tags are your friend. Know them and use them.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 28, 2006 2:53 am 
Beginner
Beginner

Joined: Thu Apr 20, 2006 3:44 am
Posts: 32
Thanks for the tips....but could you please also provide me with few justifications also for individual approach....

Thanks a lot in advance

sudhir h


Top
 Profile  
 
 Post subject:
PostPosted: Sun Apr 30, 2006 5:19 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
These are just personal opinions:

If you can't think of a good reason to use Criteria, then use HQL. It should be your first choice most of the time, as once you have the query working, there are fewer things that can go wrong. It's also the most powerful, as there are some things that Critieria just can't do (e.g. theta-style joins).

If you need to allow searching from a part of your code that isn't allowed use Hibernate classes, consider using query by example. All you have to do is instantiate an object and fill in a few "example" values, then pass it down to a part of your code that does use Hibernate.

Otherwise, use Crtieria.

_________________
Code tags are your friend. Know them and use them.


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 01, 2006 12:00 pm 
Newbie

Joined: Sun Aug 28, 2005 10:54 pm
Posts: 14
All the points mentioned in this thread are excellent. Here are my thoughts:

Some of my colleagues and I have adopted the approach of using the ICriteria interface when possible. We like having a bit of compile-time error checking for our queries and find that in some cases, we can factor common criteria into separate methods.

As mentioned, HQL is more expressive. Thus, we try to model our queries using ICriteria and fall back to HQL when appropriate. Most of our queries are fairly straight forward, with the exception of some that use GROUP BY commands.


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