Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Will hibernate java programs have no sql code?

I haven't worked with hibernate. I have little bit of experience in java. I was going through source of a beast of an java application created by Oracle(Retail Price Management). I was expecting a lot of sql code embedded in there as the application makes heavy use of database. But to my surprise, NO embedded SQL code! so far. I found that it was using what is called as "Hibernate" from the lot of .hbm.xml files. Is it a trademark for java programs using hibernate or maybe I haven't seen the complete codebase?. Could someone enlighten me how this is possible?. Thanks.

like image 306
wowrt Avatar asked Jun 11 '10 12:06

wowrt


2 Answers

Hibernate, as all ORM tools, indeed lessens or eliminates the need to use raw SQL in Java code, due to the following:

  • many associations between various entities are recorded in the Hibernate mapping, so these are fetched automatically by Hibernate - i.e. if you have an aggregation relationshiop between two classes on the Java side, this may be mapped as a foreign key relationship in the DB, and Hibernate, whenever an instance of class A is loaded, can automatically load the associated instances of class B too,
  • many queries can be done in Hibernate's own HQL query language, or using its Criteria API.

Under the hood Hibernate does generate SQL to communicate with the DB, but this is not visible on the Java side. It can be seen in the logs though, if it is enabled.

Due to this, programs using Hibernate very rarely need to use JDBC or SQL directly. The exceptions are typically ralted to "tricky" legacy DB schemas which can't be fully handled by Hibernate.

like image 101
Péter Török Avatar answered Oct 08 '22 22:10

Péter Török


Because that's the whole purpose of using Hibernate or any other object-relational mapping framework.

Hibernate solves object-relational impedance mismatch problems by replacing direct persistence-related database accesses with high-level object handling functions.

like image 42
b.roth Avatar answered Oct 08 '22 22:10

b.roth