JPA 和 Hibernate 有什么区别? [英] What's the difference between JPA and Hibernate?

查看:34
本文介绍了JPA 和 Hibernate 有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道 JPA 2 是一个规范,而 Hibernate 是一个 ORM 工具.另外,我知道 Hibernate 比 JPA 2 有更多的特性.但从实际的角度来看,真正的区别是什么?

I understand that JPA 2 is a specification and Hibernate is a tool for ORM. Also, I understand that Hibernate has more features than JPA 2. But from a practical point of view, what really is the difference?

我有使用 iBatis 的经验,现在我正在尝试学习 Hibernate 或 JPA2.我拿起了 Pro JPA2 书,它一直提到JPA 提供者".例如:

I have experience using iBatis and now I'm trying to learn either Hibernate or JPA2. I picked up Pro JPA2 book and it keeps referring to "JPA provider". For example:

如果你认为一个功能应该标准化,你应该说出来并向您的 JPA 提供商请求

If you think a feature should be standardized, you should speak up and request it from your JPA provider

这让我很困惑,所以我有几个问题:

This confuses me so I have a few questions:

  • 单独使用 JPA2 可以通过简单地注释我的 POJO 来从数据库中获取数据
  • JPA2 是否应该与JPA 提供程序"(例如 TopLink 或 Hibernate)一起使用?如果是这样,那么与单独使用 JPA2 或单独使用 Hibernate 相比,使用 JPA2 + Hibernate 有什么好处?
  • 你能推荐一本很好的实用 JPA2 书籍吗?Pro JPA2"似乎更像是一本关于 JPA2 的圣经和参考(直到本书的后半部分才进入查询).有没有介绍 JPA2 问题/解决方法的书?

推荐答案

正如您所说的 JPA 只是一个规范,这意味着没有实现.您可以使用 JPA 注释尽可能多地注释您的类,但是如果没有实现,什么都不会发生.将 JPA 视为必须遵循的准则或接口,而 Hibernate 的 JPA 实现是满足 JPA 规范定义的 API 并提供底层功能的代码.

As you state 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.

当您将 Hibernate 与 JPA 结合使用时,您实际上是在使用 Hibernate JPA 实现.这样做的好处是您可以将 Hibernate 的 JPA 实现换成 JPA 规范的另一个实现.当您直接使用 Hibernate 时,您会锁定实现,因为其他 ORM 可能使用不同的方法/配置和注释,因此您不能只是切换到另一个 ORM.

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.

有关更详细的说明,请阅读我的博客条目.

For a more detailed description read my blog entry.

这篇关于JPA 和 Hibernate 有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆