11

我发现了很多类似的问题

但没有人回答我的下一个问题。使用经典休眠方法org.hibernate.SessionFactory和 JPAjavax.persistence.EntityManager实现之间有什么区别?我听说,这个 JPA 实现使用org.hibernate.SessionFactory并像包装器一样工作,是真的吗?

4

4 回答 4

17

确实。

JPA 只是一个 API,它允许您从使用的持久层中抽象出来。Hibernate 提供了一个EntityManager接口的实现,adapter它使用与 hibernate 相同的底层方法SessionManager

例如,您可以将实现切换到 Eclipse Link,而不必更改任何源代码。

于 2013-02-21T12:53:24.977 回答
4

JPA 只是一个规范,这意味着没有实现。您可以使用 JPA 注释尽可能多地注释您的类,但是如果没有实现,则不会发生任何事情。将 JPA 视为必须遵循的准则或接口,而 Hibernate 的 JPA 实现是符合 JPA 规范定义的 API 并提供底层功能的代码。

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.

于 2014-11-12T05:26:43.517 回答
2

这是你的问题的答案


使用org.hibernate.SessionFactory 和 JPA javax.persistence.EntityManager实现的经典休眠方法有什么区别

org.hibernate.SessionFactory 

如果您将 undeline ORM 更改为 IBatis(例如),您还需要更改代码。

javax.persistence.EntityManager 

如果将 undeline ORM 更改为 IBatis(例如),则无需更改代码。

于 2013-02-21T12:45:19.067 回答
2

对于你的第一个问题,

JPA 是一种 Java API 规范,它描述了使用 Java 平台对应用程序中的关系数据进行管理。其中 Hibernate 是一个遵循 JPA 规范的 ORM(对象关系映射)库。

您可以将 JPA 视为一组由 Hibernate 实现的规则。

并回答你的第二个问题,

由于 JPA 只是一个抽象的持久层,它需要实现。而 Hibernate 实现了使用 hibernate SessionManager 的 EntityManager 接口。

通过这种方式,您完全脱离了实现方式,意味着您可以随时切换到 Hibernate 或 OenJPA 或任何其他方式,无需额外的代码更改。

于 2014-02-12T09:28:28.487 回答