aspose file tools*
The moose likes Object Relational Mapping and the fly likes Reason for using HQL in Hibernate Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Reason for using HQL in Hibernate" Watch "Reason for using HQL in Hibernate" New topic
Author

Reason for using HQL in Hibernate

Ravi Kiran Va
Ranch Hand

Joined: Apr 18, 2009
Posts: 2234

Hi , Please tell me what is the reason for using HQL , when the same thing can be done with the help of normal SQL query.

Sessions createSQLQuery()


Thanks in advance .


Save India From Corruption - Anna Hazare.
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

You do understand what Hibernate is for, don't you? The various databases all implement their own flavours of SQL.


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Ravi Kiran Va
Ranch Hand

Joined: Apr 18, 2009
Posts: 2234

Homogeneous is the word .


The reason for posting this question is that somewhere i read "HQL improves performance " when compared to SQL .

I got confusion and wanted to ask how using HQL improves performance as its internally converted to SQL .


Anyway thanks Paul .

Kieren Johnson
Greenhorn

Joined: Jul 03, 2007
Posts: 4
HQL does not refer directly to tables in the database but instead refers to the classes that these tables are mapped to in your hibernate mapping files (or annotations). This means that your code does not need to know anything about the database that it is attached to and this is a good thing.

If you decided to port your application to another database system and, in a fit of madness, to change all the table names then all you need to change in your application are the hibernate mappings, you wouldn't need to touch any part of your code. Neat, huh?
Hong Anderson
Ranch Hand

Joined: Jul 05, 2005
Posts: 1936
Using SQL defeat the purpose of ORM, actually the question should be why use SQL in Hibernate?

IMO, it's pity to allow to use SQL in ORM tools, what is the purpose of ORM again?


SCJA 1.0, SCJP 1.4, SCWCD 1.4, SCBCD 1.3, SCJP 5.0, SCEA 5, SCBCD 5; OCUP - Fundamental, Intermediate and Advanced; IBM Certified Solution Designer - OOAD, vUML 2; SpringSource Certified Spring Professional
Hong Anderson
Ranch Hand

Joined: Jul 05, 2005
Posts: 1936
Kieren Johnson wrote:HQL does not refer directly to tables in the database but instead refers to the classes that these tables are mapped to in your hibernate mapping files (or annotations). This means that your code does not need to know anything about the database that it is attached to and this is a good thing.

If you decided to port your application to another database system and, in a fit of madness, to change all the table names then all you need to change in your application are the hibernate mappings, you wouldn't need to touch any part of your code. Neat, huh?

The main problem is not table names, but SQL dialect. Different RDBMS use different SQL dialects.
That is the reason why Hibernate introduced HQL, SQL will be generated by Hibernate, so if we want to change RDBMS, we just change dialect instead of take a look at all SQL statements and convert SQL from dialect A to dialect B.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Reason for using HQL in Hibernate