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

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

问题描述

据我所知,JPA 2是一个规范,而Hibernate是ORM的一个工具。另外,我明白Hibernate比JPA 2具有更多的功能。但从实际角度来看,真正的区别是什么?

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


如果您认为某个功能应该标准化,您应该说出
并从您的JPA提供商

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


  • 仅使用JPA2,我可以通过简单地注释POJO来获取数据。

  • JPA2是否应该与JPA Provider(例如TopLink或Hibernate)一起使用?如果是这样,那么与单独使用JPA2相比,使用JPA2 + Hibernate有什么好处?或者与单独使用Hibernate相比?
  • 您能推荐一本好实用的JPA2书吗? Pro JPA2似乎更像是一本关于JPA2的圣经和参考资料(直到本书后半部分才进入查询)。有没有一本书能够解决JPA2问题/解决方案?
  • JPA只是一个规范,意味着没有实现。您可以使用JPA批注尽可能多地注释类,但是如果没有实现,则不会发生任何事情。将JPA视为必须遵循的准则或接口,而Hibernate的JPA实现是符合JPA规范定义的API的代码,并提供了引擎盖下的功能。



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



    更详细的说明请阅读我的博客条目


    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?

    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:

    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:

    • Using JPA2 alone can I fetch data from DB by simply annotating my POJO's
    • Is JPA2 supposed to be used with a "JPA Provider" e.g TopLink or Hibernate? If so, then what's the benefit of using JPA2 + Hibernate as compared to JPA2 alone, or compared to Hibernate alone ?
    • Can you recommend a good practical JPA2 book. "Pro JPA2" seems more like a bible and reference on JPA2 (It doesn't get into Queries until the later half of the book). Is there a book that takes a problem/solution approach to JPA2?

    解决方案

    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.

    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天全站免登陆