wood burning stoves 2.0*
The moose likes Object Relational Mapping and the fly likes Query Execution in Hibernate Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Query Execution in Hibernate" Watch "Query Execution in Hibernate" New topic
Author

Query Execution in Hibernate

Vaibhav G Garg
Ranch Hand

Joined: Sep 23, 2011
Posts: 140
Hi,

We are developing a web based application using Hibernate ORM.

For optimal performance, which of the following way we should be using for DB Queries:

i. Native SQL

ii. Criteria Query

iii. Named Queries

iv. HQL

Please reply at the earliest with the pros/cons of each one.

Thanks,
vaibhav garg
Bill Gorder
Bartender

Joined: Mar 07, 2010
Posts: 1646
    
    7

-Use the native SQL for things that hibernate was not really built for like batch queries (it can be done but its not what an ORM is designed for) or in some cases Stored Procedures.
-Prefer Criteria queries for dynamic queries. In this case there is no need to to parse before generating. This makes it superior to concatenating HQL together, using a string builder (this should be avoided). They are also type safe if you are using the JPA criteria API.
-Named queries are the best performing as they are generated and parsed only once. Use these for frequent queries that are not dynamic. HQL also has the benefit of being easier to read than criteria queries.
-Non-named queries must be parsed and generated each pass, for infrequent queries this is fine, but otherwise I would go with one of the other options.




[How To Ask Questions][Read before you PM me]
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Query Execution in Hibernate
 
Similar Threads
JPA Query and HQL
Hibernate - Query language independency
migrating code from hibernate 2 to hibernate 3.0
Where are hibernate queries generally maintained
trouble using BINARY in where clause with Hibernate 3.1