File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Object Relational Mapping and the fly likes Hibernate - Criteria Query V/S HQL Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Hibernate - Criteria Query V/S HQL" Watch "Hibernate - Criteria Query V/S HQL" New topic
Author

Hibernate - Criteria Query V/S HQL

Vaibhav G Garg
Ranch Hand

Joined: Sep 23, 2011
Posts: 132
In Hibernate, I would like to know in which scenario we should be using Criteria Query and in which scenario, we should prefer using HQL. Please provide your inputs.
Balaji Vankadaru
Ranch Hand

Joined: May 31, 2013
Posts: 47
The Best place for using Hibernate Criteria would be when ever you want to lay restrictions or have an aggregate operations performed or doing any search kind of functionality.


eg:- Lets say you have a Employee class and you have say 10 columns for the Employee Table. While fetching records from the database you need certain conditions to meet say age should be greater than 25 and salary should me less than 25k and location should be from India ...!

This can be done using hql too but hibernate comes with handy implementations which are easy to write and less error pron than hql.

Hql is the best place to use when ever you want to perform normal calls to the database.

Eg: Performing crud operations or executing a stored procedure.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Hibernate - Criteria Query V/S HQL
 
Similar Threads
HQL and Criteria API
Hibernate Caching Query
hibernate subqueries
org.hibernate.QueryParameterException: could not locate named parameter
When we choose SQL,HQL and Criteria API of hibernate?