简体   繁体   English

Hibernate和Hibernate JPA之间的区别

[英]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?使用org.hibernate.SessionFactory和 JPA javax.persistence.EntityManager实现的经典 hibernate 方法有什么区别? I heard, that this JPA implementation uses org.hibernate.SessionFactory and works like wrapper, is it real?我听说,这个 JPA 实现使用org.hibernate.SessionFactory并且像包装器一样工作,是真的吗?

Indeed. 确实。

JPA is simply an API that allows you to abstract from the used persistence layer. JPA只是一个API,允许您从使用的持久层中进行抽象。 Hibernate provides an implementation of the EntityManager interface that acts as an adapter - it uses the same underlying methods as a hibernate SessionManager . Hibernate提供了充当adapterEntityManager接口的实现-它使用与休眠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. 这个想法是,例如,您可以将实现切换到Eclipse Link,而不必更改任何源代码。

JPA is just a specification, meaning there is no implementation. JPA只是一个规范,意味着没有实现。 You can annotate your classes as much as you would like with JPA annotations, however without an implementation nothing will happen. 您可以根据需要使用JPA批注对类进行批注,但是如果没有实现,将不会发生任何事情。 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. 将JPA视为必须遵循的准则或接口,而Hibernate的JPA实现是符合JPA规范定义的API并提供幕后功能的代码。

When you use Hibernate with JPA you are actually using the Hibernate JPA implementation . 当将Hibernate与JPA一起使用时,实际上是在使用Hibernate JPA实现 The benefit of this is that you can swap out Hibernate's implementation of JPA for another implementation of the JPA specification. 这样做的好处是,您可以将Hibernate的JPA实现换成JPA规范的另一种实现。 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. 当您使用直接的Hibernate时,由于其他ORM可能使用不同的方法/配置和注释,因此您将锁定在实现中 ,因此您不能仅切换到另一个ORM。

To your First question, 第一个问题,

JPA is a Java API specification which describes the management of relational data in applications using Java Platform. JPA是Java API规范,它描述了使用Java平台的应用程序中关系数据的管理。 where as Hibernate is a ORM (Object Relational Mapping) library which follows JPA specification. 其中Hibernate是遵循JPA规范的ORM(对象关系映射)库。

You can think JPA as a set of Rules which is implemented by Hibernate. 您可以将JPA视为由Hibernate实现的一组规则。

And answer for your second question, 然后回答第二个问题,

As JPA is just an abstracted persistence layer it requires implementation. 由于JPA只是一个抽象的持久层,因此需要实现。 and Hibernate implements EntityManager interface that uses hibernate SessionManager. Hibernate实现了使用hibernate SessionManager的EntityManager接口。

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. 这样,您就完全脱离了实现方式,这意味着您可以随时切换到Hibernate或OenJPA或任何其他方式,而无需更改其他代码。

Here is the answer of you question 这是您问题的答案

What diference between classical hibernate approach using 经典休眠方法使用之间有什么区别
org.hibernate.SessionFactory and JPA javax.persistence.EntityManager org.hibernate.SessionFactory和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. 如果将未定界的ORM更改为IBatis(例如),则还需要更改代码。

javax.persistence.EntityManager 

if you change the undeline ORM to IBatis(for eg) you dont need to change the code. 如果将未定界的ORM更改为IBatis(例如),则无需更改代码。

Keyword关键词 JPA JPA Hibernate Hibernate
Location地点 Described in javax.persistence package javax.persistence package 中描述 Described in org.hibernate org.hibernate中描述
Intent意图 It's only Java speicification仅 Java 规范 It's is an implementation of JPA.它是 JPA 的实现。
Functionality功能性 It describes the handling of relational data in Java applications它描述了 Java 应用程序中关系数据的处理 Hibernate is an Obejct relational mapping (ORM) tool that is used to save the Java objects in the relational database system Hibernate是一个对象关系映射(ORM)工具,用于在关系数据库系统中保存Java对象
Standard API标准 API It's a standard API that permits to perform database operations这是一个允许执行数据库操作的标准 API It is used in mapping Java data types with SQL data types and database tables用于映射 Java 数据类型与 SQL 数据类型和数据库表
CRUD action CRUD 动作 To perform CRUD actions for instances of mapped entity classes, it uses EntityManager interface which is supplied by EntityManagerFactory为了对映射实体类的实例执行 CRUD 操作,它使用EntityManager EntityManagerFactory To perform CRUD actions for instances of mapped entity classes, it uses Session interface which is supplied by SessionFactory为了对映射实体类的实例执行 CRUD 操作,它使用SessionFactory提供的Session接口

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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