Hibernate Vs. JPA
In this article, I discuss the difference between Hibernate and JPA, and the use cases made of these technologies on (old and new) projects; the history is important.
Join the DZone community and get the full member experience.Join For Free
In this article, I would like to discuss the difference between Hibernate and JPA, and the use cases made of these technologies on (old and new) projects; the history is important.
Then we will deduce the orientations that we wish to give on modern projects.
A Bit of History
If you learned the web as I did in the early 2000s, you built your first PHP apps building personal pages. If you needed non-static content on your pages, then the host provided you with access to a PostgreSQL database. To interact with this database, we launched SQL queries from PHP, and voila.
Many of the new developers entering the market are not trained in SQL or relational basics, as if they no longer need to know this concept.
Indeed, in the meantime, procedural languages have become object-oriented, starting with the language of the Internet: PHP. As the object dominates the world, we have sought to abstract ourselves from the relational: the bases have also become objects. We thus speak of the principle of object-relational mapping, ORM. This approach assumes that objects exist in a programming language, that tables and relationships exist in a database, and that a mapping exists allowing the translation between the two universes.
We are in 1995 when this turning point begins to take place. The first successful ORM was TopLink. Toplink was originally developed by 'The Object people of Smalltalk.' It was ported to Java in 1996-1998 and called "TopLink for Java." Later the TopLink code was donated to the Eclipse Foundation and the EclipseLink project was born in 2007.
Along with the development of TopLink, Sun exported TopLink interfaces to create the 'JPA' standard, the first release of which dates from May 2006. These interfaces are therefore integrated into Java in the 'javax.transaction' package. This way, Sun also introduces a concept close to ORM in Java: persistence. Saying that objects persist will mean that we no longer want to simply save data but to persist the states of objects used by a program. Also, on the basis of these interfaces, a new language is also introduced 'JPQL': it allows to query persisted objects.
When you have JPA and JPQL and the relational model is already present, as well as the mapping, you no longer need to know SQL and relational bases. In order to be able to completely put these old technologies in the basement, we still had to be able to give full control to the object, to automate the maintenance of the relational model and the mapping.
In order to be able to establish the JPA standard released in 2006 from TopLink, Sun Microsystems had in fact entrusted a group of expert partner companies (JCP) with the reflection and design of the project. Among them was a representative from JBoss. In competition, JBoss created an ORM framework for Java. It was Hibernate who arrived in 2001.
TopLink also allowed object persistence to file (XML), or to service call (SDO). Hibernate focuses on object-relational mapping, is open to all database engines, and it provides all of the missing query functionality that TopLink doesn't, including one language: HQL.
In your experience, you may have encountered company applications on which hibernate was embedded. And sometimes JPA is not yet dominant. It means that what they use is the Hibernate framework. There are hbm.xml mappings, basic session management with Hibernate, use of Hibernate utilities. Hibernate existed before JPA and was of course more comprehensive for a long time, which explains its mass adoption, especially in the years 2000.
Since that time, JPA and Hibernate have evolved and transformed. For those who have not followed all the episodes of the series, it is legitimate to wonder what the difference today is between the two and which solution is more relevant in the market.
As mentioned before, unlike TopLink and Hibernate, JPA is just an ORM standard, not on its own. This is the most remarkable difference, whereas it is possible to use only hibernate (code, configuration, annotations, engine, etc.). That said, when JPA proposes its standard, Hibernate (v3.2) is one of the engines that will implement it, just like TopLink, OpenJPA, ObjectDB, etc.
JPA being the standard and the engines being multiple, it becomes more logical to follow the standard to be able to easily change or update its engine (Hibernate).
Last important point, but not least, there is a lag between JPQL and HQL, behavioral issues can arise if you generate your queries with hibernate, which doesn't happen if you generate queries with JPA.
Opinions expressed by DZone contributors are their own.