Difference between HQL and Criteria Query in Hibernate
|
Hibernate »
On Jul 14, 2011 | { 6 Comments }
|
Tweet
|
Let us see the main differences between HQL and Criteria Query
- HQL is to perform both select and non-select operations on the data, but Criteria is only for selecting the data, we cannot perform non-select operations using criteria
- HQL is suitable for executing Static Queries, where as Criteria is suitable for executing Dynamic Queries
- HQL doesn’t support pagination concept, but we can achieve pagination with Criteria
- Criteria used to take more time to execute then HQL
- With Criteria we are safe with SQL Injection because of its dynamic query generation but in HQL as your queries are either fixed or parametrized, there is no safe from SQL Injection.
|
What you are thinkig....
6 Responses to “Difference between HQL and Criteria Query in Hibernate”
If you want a pic to show with your comment, go get a gravatar !
Please post your questions on Java4s Answers forum



hai sir,
thanks a lot for ur lovly heart this all concepts very usefull
@ramu
you bet, thanks for your feedback
I wont agree with pagination concept given by you
please refer below
Hibernate APIs provide few methods to set the pagination citerias in the query. We can use bothe normal Query and Criteria API. Look into the following code for how to create pagination using the Query object:
Query query = session.createQuery(“from Studenet s”);
query.setFirstResult(25);
query.setMaxResults(50);
For Criteria :
Criteria criteria = session.createCriteria(Student.class);
criteria.setFirstResult(25);
criteria.setMaxResults(50);
List result = criteria.list();
Thanks a ton for the valuable information.
Hi,
Can u please explain select and non-select operations with example in both hql and criteria queries.i am nt clear with the above statement.
Thanks,
Mahi
“HQL is suitable for executing Static Queries”. HQL is also suitable for executing dynamic
queries by using Query.setParameter()mthod. In that method we can able to pass the values
at run time.so how we can able to say HQL is suitable to execute static query?.