Reputation: 303
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?
Upvotes: 13
Views: 9777
Reputation: 1715
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 |
Upvotes: 0
Reputation: 51
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.
Upvotes: 5
Reputation: 4188
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.
Upvotes: 2
Reputation: 61148
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.
Upvotes: 17
Reputation: 3673
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 e.g) you need to change the code as well.
javax.persistence.EntityManager
if you change the undeline ORM to IBatis(for e.g) you dont need to change the code.
Upvotes: 2