views:

652

answers:

3

what is similarity and difference between jpa and hibernate.

+2  A: 

JPA (Java Persistence API) is an interface for persistence providers to implement. Hibernate is one such implementation of JPA.

Jeff Foster
+5  A: 

This is the introduction of the JSR-000220 Enterprise JavaBeans 3.0 Final Release (persistence):

This document is the specification of the Java API for the management of persistence and object/rela- tional mapping with Java EE and Java SE. The technical objective of this work is to provide an object/relational mapping facility for the Java application developer using a Java domain model to man- age a relational database.

This persistence API—together with the query language and object/relational mapping metadata defined in this document—is required to be supported under Enterprise JavaBeans 3.0. It is also targeted at being used stand-alone with Java SE.

Leading experts throughout the entire Java community have come together to build this Java persistence standard. This work incorporates contributions from the Hibernate, TopLink, and JDO communities, as well as from the EJB community.

In other words, JPA is the standardized API for persistence. Hibernate provides an implementation of the standard (i.e. it can be used as underlying persistence engine when using JPA).

Prior to JPA, Hibernate was a kind of de facto standard for object persistence in Java. Hibernate is considered as a major contributor to JPA so there are lots of similarities between them (I could even say between all ORM solutions as ORM concepts are common). However, for various reasons, political or technical, the JPA working group couldn't include everything in the first release of JPA so JPA is actually a subset of Hibernate (this is a simplified view but the reality is very close). Hibernate and other JPA implementation provide thus their own proprietary extensions (which means non standards i.e. not portable from one provider to the other) to the JPA standard that you may use, or not.

Pascal Thivent
+3  A: 

As pointed out by @Pascal Hibernate existed prior to JPA standard(it is now JSR 317 JPA 2.0, which Hibernate has implemented in 3.5 already out for early use). So there are other providers of JPA e.g. Oracle TopLink, Apache OpenJPA. To use strictly JPA in Hibernate you have to use EntityManager as apposed to the Session which is a Hibernate concept pre-dating JPA. Similarly for strict JPA you have to use EntityManagerFactory as apposed to SessionFactory.

The EntityManager and EntityManagerFactory are, in fact, thin wrappers around the Session and SessionFactory respectively. In addition, core/annotations Hibernate provide extensions to the JPA spec that make your life easier. Before JPA 2 Hibernate had a Criteria API which allowed you to programmatically construct a query in an OO fashion. JPA 2 now makes this functionality standard -- the JPA standard is a bit different from the Hibernate version as it employs generics. So basically the Hibernate functionality is a superset of JPA.

non sequitor
Indeed, Hibernate has the `Session` but this is not really a concept, it's just an implementation of the *unit of work* which is a shared ORM concept (Toplink has the `UnitOfWork`, JDO has the `PersistenceManager`, Hibernate has the `Session`).
Pascal Thivent