wood burning stoves
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 Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Query Execution in Hibernate" Watch "Query Execution in Hibernate" New topic

Query Execution in Hibernate

Vaibhav G Garg
Ranch Hand

Joined: Sep 23, 2011
Posts: 141

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.

vaibhav garg
Bill Gorder

Joined: Mar 07, 2010
Posts: 1682

-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]
I agree. Here's the link: http://aspose.com/file-tools
subject: Query Execution in Hibernate
It's not a secret anymore!