Newsletter |
Difference between HQL and Criteria Query in Hibernate
Hibernate » on Jul 14, 2011 { 23 Comments } By Sivateja
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.
β ββ
You Might Also Like
::. About the Author .:: | ||
Comments
23 Responses to “Difference between HQL and Criteria Query in Hibernate”
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 this the correct information.
yes this is correct
we can also achieve pagination via Query.
Thanks for providing correct information
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
Not select means update or delete or insert, We can do it in HQL.
“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?.
query is static but we just give the parameterised values either names or positional parameter but here passing dynamically values only not query , then why crieteria we said dynamic bcause we just give methods but internally Hibernate generate query and fetching the values
how can i use joins in criteria concepts?
Thanks alot SivaTeja…
this is very good meterial for hibernate.
easily understand….
Hi,
In your last point you mentioned about sql injection.
could you please explore it with proper example.
we can write dynamic quries using hql, but u are telling HQL is suitable for executing Static Queries, where as Criteria is suitable for executing Dynamic Queries
completely analysed,,,,,great thanx to you….
Hi,
I have one doubt, Why HQL does not support pagination?
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();
Good article.
super sir…..thanks a lot.
Thanks for everything sir
Q.can we perform CURD operation by using QBC.?
which is better as per performance or speed of operation?