简体   繁体   中英

Difference between Hibernate and Hibernate JPA

I found a lot of similar questions

but no one answers my next question. What diference between classical hibernate approach using org.hibernate.SessionFactory and JPA javax.persistence.EntityManager implementation? I heard, that this JPA implementation uses org.hibernate.SessionFactory and works like wrapper, is it real?

Indeed.

JPA is simply an API that allows you to abstract from the used persistence layer. Hibernate provides an implementation of the EntityManager interface that acts as an adapter - it uses the same underlying methods as a hibernate SessionManager .

The idea is that you could, for example, switch your implementation to Eclipse Link and not have to change any of your source code.

JPA is just a specification, meaning there is no implementation. You can annotate your classes as much as you would like with JPA annotations, however without an implementation nothing will happen. Think of JPA as the guidelines that must be followed or an interface, while Hibernate's JPA implementation is code that meets the API as defined by the JPA specification and provides the under the hood functionality.

When you use Hibernate with JPA you are actually using the Hibernate JPA implementation . The benefit of this is that you can swap out Hibernate's implementation of JPA for another implementation of the JPA specification. When you use straight Hibernate you are locking into the implementation because other ORMs may use different methods/configurations and annotations, therefore you cannot just switch over to another ORM.

To your First question,

JPA is a Java API specification which describes the management of relational data in applications using Java Platform. where as Hibernate is a ORM (Object Relational Mapping) library which follows JPA specification.

You can think JPA as a set of Rules which is implemented by Hibernate.

And answer for your second question,

As JPA is just an abstracted persistence layer it requires implementation. and Hibernate implements EntityManager interface that uses hibernate SessionManager.

In this way, you are completely detached from the implementation way, means you can switch to any of Hibernate or OenJPA or any other whenever you want, no additional code changes required.

Here is the answer of you question

What diference between classical hibernate approach using
org.hibernate.SessionFactory and JPA javax.persistence.EntityManager
implementation?

org.hibernate.SessionFactory 

if you change the undeline ORM to IBatis(for eg) you need to change the code as well.

javax.persistence.EntityManager 

if you change the undeline ORM to IBatis(for eg) you dont need to change the code.

Keyword JPA Hibernate
Location Described in javax.persistence package Described in org.hibernate
Intent It's only Java speicification It's is an implementation of JPA.
Functionality It describes the handling of relational data in Java applications Hibernate is an Obejct relational mapping (ORM) tool that is used to save the Java objects in the relational database system
Standard API It's a standard API that permits to perform database operations It is used in mapping Java data types with SQL data types and database tables
CRUD action To perform CRUD actions for instances of mapped entity classes, it uses EntityManager interface which is supplied by EntityManagerFactory To perform CRUD actions for instances of mapped entity classes, it uses Session interface which is supplied by SessionFactory

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM