本文作者:苏生米沿

本文地址:http://blog.csdn.net/sushengmiyan/article/details/50182005

翻译来源:http://stackoverflow.com/questions/20820880/hibernate-native-vs-hibernate-jpa

Hibernate 官方网站说,有native Hibernate API和 Hibernate 的JPA实现。在这两者之间有什么区别呢?优势劣势都是什么?

Hibernate website says there is a native Hibernate API as well as an implementation of JPA. What is the difference between the Native API and JPA implementation? Advantages, disadvantages?

在使用Spring MVC构建应用程序的时候,使用tomcat做为服务器,使用mysql作为持久化数据库,对spring来说,我是新手,并且没有使用过Hibernate,我的团队想通过使用ORM方案,并且比较来说,Hibernate比较流行。现在不确定,Hibernate 是如何工作的?或者,我该使用native Hibernate或者是hibernate的JPA实现?应用程序是数据驱动的,实体和报表展示的。

I am working on a Spring MVC application, using Tomcat as the container, and MySQL for persistence. I'm newer to Spring and never used Hibernate. My team would like to use an ORM and Hibernate seems to be the most popular. We're not sure how Hibernate is going to workout or whether we should use native or JPA api. The application will be data driven, data entry, reporting, etc.

我知道,使用JPA的话,对于程序切换到另一个JPA实现会比较容易,尽管我不知道这样做是否是需要的。

I've read that using JPA makes its easier to switch to another JPA implementation, although I don't know if that will be needed or not.

JPA是通过ORM访问关系数据库的一个标准,Hibernate是它的一个实现。当年想使用JPA的时候,你需要使用它的一个实现,hibernate是一个不错的选择,但是还有其它实现,比如EclipseLink。

JPA is a standard for accessing relational databases through an object oriented API. Hibernate is an implementation of this API. When you want to use JPA you need some vendor to implement it, Hibernate is a good choice but there are others like EclipseLink.

Hibernate 存在的时间比JPA要长久。陈旧的native API仍然存在,并且提供一些标准的JPA没有提供的功能,如果你需要使用这些的话你就选择native API,使用JPA的话,就是更多程序员了解它,并且也可以通过配置来使用这些特性。

Hibernate exists longer than JPA. The native, older API (which was a model for JPA) still exists, and sometimes it offers more possibilities than are exposed through JPA (e.g. orphan removal). If you need those you need to use the native API. Using JPA has other merits, most important (in my opinion) more developers that know it. And you still can use some Hibernate specifics through configuration.

大多数的使用native hibernate的体验都是陈旧的,像Hibernate 3,是JPA的发行前版本.如果你是刚开始,建议使用JPA开始,尽管native的也有很多好的使用原由。

Most tutorials that use Hibernate natively are quite old - as is Hibernate 3, a pre-JPA release. While there are good reasons to use it, they (IMO) typically don't apply to the general audience. So if you are just beginning to learn in this field I would suggest to start with JPA.

作为线下资源,这里并没有何时的话题,你需要借鉴官方文档作为一个开始,并且至少从Hibernate4开始体验。

As for recommendations on offsite resources: For good reasons they are not on topic here. But thecurrent official Hibernate documentation would be a good start, as would be to look for toturial for at least Hibernate 4.

翻译来源:http://www.javabeat.net/jpa-entitymanager-vs-hibernate-sessionfactory/

If you are using the JPA’s standard specification implementation (Read : Introduction to JPA), then you would use EntityManagerFactory for opening the session. But, if you are using the hibernate implementation, you have hibernate specific SessionFactory for managing the sessions. Here there is lot of confusion between developers like which one is the best approach. Here, there is two opinions are popular:

如果你在使用标准的JPA实现的话,你可能会使用EntityManagerFactory来打开一个session,但是,如果你使用hibernate的实现的话,你使用hibernate的特定的SessionFactory来管理你的session,开发者在选择两者的时候有很多疑惑的,这里有两个主要观点:

  1. EntityManagerFactory is  the standard implementation, it is the same across all the implementations. If we migrate our ORM for any other provider, there will not be any change in the approach for handling the transaction. In contrast, if you use hibernate’s session factory, it is tied  to hibernate APIs and ca not migrate to new vendor easily.
1.使用EntityManager是一个标准实现,你可以在所有实现中通用。如果我们更换其他ORM框架,不存在任何困难,但是你使用hibernate的话,那就绑定了,不能轻易的转换到其它实现了。

2.One dis-advantage of using the standard implementation is that, it is not providing the advanced features. There is not much control provided in the EntityManager APIs. Whereas, hibernate’s SessionFactory has lot of advanced features which can not done in JPA. One such thing is retrieving the ID generator without closing the transaction, batch insert, etc.
使用这个的一个缺点就是她不能提供一些hibernate的一些特性,比如,使用回本儿的SessionFactory不关闭事务来获取ID等。
Looking into the above points, one has to decide which one is better. There is no hard rule, after all it depends on the developers requirement. Another suggestion is that, we can use entity manger and session factory together. In this approach, entity manage delegates session handling to the hibernate by invoking the unwrap method. Like this:
比较而言,没有绝对的谁好谁不好,需要自己定夺。毕竟需要根据开发环境选择的,一个建议就是我们使用entitymager和sessionfactory一起,这样的话,我们就能融合两者了。
可以通过如下方法切换:Session session = entityManager.unwrap(Session.class);
Using EntityManagerFactory approach allows us to use callback method annotations like @PrePersist, @PostPersist,@PreUpdate with no extra configuration. Using similar callbacks while usingSessionFactory will require extra efforts.
使用EntityManager可以让我们通过注解使用回调方法@PrePersist, @PostPersist,@PreUpdate而hibernate则需要额外的工作。

Native Hibernate与Hibernate JPA的更多相关文章

  1. Hibernate注解与JPA

    Hibernate注解与JPA - charming的专栏 - CSDN博客http://blog.csdn.net/zxc123e/article/details/51499652 之前记录的一些东 ...

  2. Hibernate之Hibernate环境搭建

    Hibernate之Hibernate环境搭建 一.Hibernate环境搭建的步骤 1.添加Hibernate && SQLServer 的Jar antlr-2.7.7.jar d ...

  3. JavaWeb_(Hibernate框架)Hibernate配置文件hibernate.cfg.xml

    hibernate.cfg.xml配置文件——链接数据库 hibernate.cfg.xml一定要配置在/src文件目录下 --数据库驱动,url,用户名,密码 --方言org.hibernate.d ...

  4. 问题Initial SessionFactory creation failed.org.hibernate.HibernateException: /hibernate.cfg.xml not found解决方法

    问题Initial SessionFactory creation failed.org.hibernate.HibernateException: /hibernate.cfg.xml not fo ...

  5. Hibernate笔记——Hibernate介绍和初次环境配置

    Hibernate简介 Hibernate是一个开放源代码的对象关系映射框架,它对JDBC进行了非常轻量级的对象封装,使得Java程序员可以随心所欲的使用对象编程思维来操纵数据库. Hibernate ...

  6. org.hibernate.HibernateException: /hibernate.cfg.xml not found等三个问题

    初次配置hibernate在myeclipse上: 出现三个问题,怎么都不好使,比对代码,没有问题,查看路径还是没有问题: 1.org.hibernate.HibernateException: /h ...

  7. 【hibernate】Hibernate中save, saveOrUpdate, persist, merge, update 区别

    Hibernate Save hibernate save()方法能够保存实体到数据库,正如方法名称save这个单词所表明的意思.我们能够在事务之外调用这个方法,这也是我不喜欢使用这个方法保存数据的原 ...

  8. 【hibernate】hibernate和mybatis的比较

    理解和学习,使自己在做项目中更加得心应手. 第一方面:开发速度的对比就开发速度而言,Hibernate的真正掌握要比Mybatis来得难些.Mybatis框架相对简单很容易上手,但也相对简陋些.个人觉 ...

  9. 【Hibernate】Hibernate的多表查询

    在<[Hibernate]Hibernate的聚类查询.分组查询.排序与时间之差>(点击打开链接)一文中已经讲述过怎样利用HQL语句代替SQL语句.进行聚类查询.分组查询.排序与时间之差的 ...

随机推荐

  1. logging格式

    import logging def foo(s): return 10 / int(s) def bar(s): return foo(s) * 2 def main(): try: bar(0) ...

  2. SpringCloud异常(Euruka):Application run failed java.lang.NoSuchMethodError: org.springframework.boot.builder.SpringApplicationBuilder

    在测试Euruka作为服务注册中心的时候碰到了这个问题,错误提示如下: "C:\Program Files\Java\jdk1.8.0_161\bin\java" -XX:Tier ...

  3. css 宽高自适应的div 元素 如何居中 垂直居中

    在我们 编写css 样式的时候经常会遇见一个问题 那就是一个 宽高未知的元素 要让他 垂直居中如何实现这个呢 下面是我常用的两种方法 上代码 下面的是 结构代码 <div class=" ...

  4. 坑:JavaScript 中 操作符“==” 和“===” 的区别

    标题:JavaScript 中 操作符"==" 和"===" 的区别 记录一些很坑的区别: 1. '' == '0' // false 0 == '' // t ...

  5. [SDOI2008]Sue的小球

    题目描述 Sue和Sandy最近迷上了一个电脑游戏,这个游戏的故事发在美丽神秘并且充满刺激的大海上,Sue有一支轻便小巧的小船.然而,Sue的目标并不是当一个海盗,而是要收集空中漂浮的彩蛋,Sue有一 ...

  6. [ZJOI2008]泡泡堂

    题目描述 第XXXX届NOI期间,为了加强各省选手之间的交流,组委会决定组织一场省际电子竞技大赛,每一个省的代表队由n名选手组成,比赛的项目是老少咸宜的网络游戏泡泡堂.每一场比赛前,对阵双方的教练向组 ...

  7. HDU 1724 Ellipse

    Problem Description Math is important!! Many students failed in 2+2’s mathematical test, so let's AC ...

  8. SAC E#1 - 一道难题 Tree

    题目背景 冴月麟和魏潇承是好朋友. 题目描述 冴月麟为了守护幻想乡,而制造了幻想乡的倒影,将真实的幻想乡封印了.任何人都无法进入真实的幻想乡了,但是她给前来救她的魏潇承留了一个线索. 她设置了一棵树( ...

  9. 习题 7-2 uva225(回溯)

    题意:从(0.0)点出发,第一次走一步……第k次走k步,且每次必须转90度,不能走重复的点.求k次后回到出发点的所有情况.按最小字典序从小到大输出. 思路: 把所有坐标+220,保证其是正数,然后搜索 ...

  10. [bzoj4866] [Ynoi2017]由乃的商场之旅

    来自FallDream的博客,未经允许,请勿转载,谢谢, 由乃有一天去参加一个商场举办的游戏.商场派了一些球王排成一行.每个人面前有几堆球.说来也巧,由乃和你一样,觉得这游戏很无聊,于是决定换一个商场 ...