Native Hibernate与Hibernate JPA
本文作者:苏生米沿
本文地址: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,开发者在选择两者的时候有很多疑惑的,这里有两个主要观点:
- 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.
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的更多相关文章
- Hibernate注解与JPA
Hibernate注解与JPA - charming的专栏 - CSDN博客http://blog.csdn.net/zxc123e/article/details/51499652 之前记录的一些东 ...
- Hibernate之Hibernate环境搭建
Hibernate之Hibernate环境搭建 一.Hibernate环境搭建的步骤 1.添加Hibernate && SQLServer 的Jar antlr-2.7.7.jar d ...
- JavaWeb_(Hibernate框架)Hibernate配置文件hibernate.cfg.xml
hibernate.cfg.xml配置文件——链接数据库 hibernate.cfg.xml一定要配置在/src文件目录下 --数据库驱动,url,用户名,密码 --方言org.hibernate.d ...
- 问题Initial SessionFactory creation failed.org.hibernate.HibernateException: /hibernate.cfg.xml not found解决方法
问题Initial SessionFactory creation failed.org.hibernate.HibernateException: /hibernate.cfg.xml not fo ...
- Hibernate笔记——Hibernate介绍和初次环境配置
Hibernate简介 Hibernate是一个开放源代码的对象关系映射框架,它对JDBC进行了非常轻量级的对象封装,使得Java程序员可以随心所欲的使用对象编程思维来操纵数据库. Hibernate ...
- org.hibernate.HibernateException: /hibernate.cfg.xml not found等三个问题
初次配置hibernate在myeclipse上: 出现三个问题,怎么都不好使,比对代码,没有问题,查看路径还是没有问题: 1.org.hibernate.HibernateException: /h ...
- 【hibernate】Hibernate中save, saveOrUpdate, persist, merge, update 区别
Hibernate Save hibernate save()方法能够保存实体到数据库,正如方法名称save这个单词所表明的意思.我们能够在事务之外调用这个方法,这也是我不喜欢使用这个方法保存数据的原 ...
- 【hibernate】hibernate和mybatis的比较
理解和学习,使自己在做项目中更加得心应手. 第一方面:开发速度的对比就开发速度而言,Hibernate的真正掌握要比Mybatis来得难些.Mybatis框架相对简单很容易上手,但也相对简陋些.个人觉 ...
- 【Hibernate】Hibernate的多表查询
在<[Hibernate]Hibernate的聚类查询.分组查询.排序与时间之差>(点击打开链接)一文中已经讲述过怎样利用HQL语句代替SQL语句.进行聚类查询.分组查询.排序与时间之差的 ...
随机推荐
- Java中的变量数据类型补充
Java中变量的数据类型的补充 变量按照数据类型进行分类 1.基本数据类型 数值型:①整数类型(byte,short,int,long)②浮点型(float,doubbe)③字符型(char)④布尔型 ...
- Spring-framework 源码导入 IntelliJ IDEA 记录
前言 想学习spring框架,不看源码怎么行.网上有很多教程,但是自己实施起来还是稍微有点坎坷的,不过最后还是成功了.遂以此文记录. 环境说明: Idea 2017.2.5 spring-frame ...
- [LeetCode] Bulb Switcher II 灯泡开关之二
There is a room with n lights which are turned on initially and 4 buttons on the wall. After perform ...
- Java入门之JDK的安装和环境变量的配置
Java的版本 1. Java SEjava se 以前称为J2SE.它允许开发和部署在桌面.服务器.嵌入式环境和实时环境中使用的 Java 应用程序.Java SE是基础包,但是也包含了支持 Jav ...
- 学习flexible.js
flexible是手淘团队总结的一套移动端适配方案,这篇文章是自己对flexible学习历程的记录,也算是一个备忘. https://github.com/amfe/article/issues/17 ...
- PHPCMS v9.6.0 wap模块 SQL注入
调试这个漏洞的时候踩了个坑,影响的版本是php5.4以后. 由于漏洞是由parse_str()函数引起的,但是这个函数在gpc开启的时候(也就是php5.4以下)会对单引号进行过滤\' . 看这里: ...
- 在Linux服务器部署 .NET-Core 项目
一.文章概要 这篇文章是讲述一个Linux 新手将 .NET-Core 项目部署在 Linux 服务器上的一个记录,以及在部署期间遇到的问题以及解决办法.有不恰当的地方.欢迎大神指正. 二.前期准备 ...
- [bzoj1558][JSOI2009]等差数列
题目:给定n个数,m个操作,每次给一段区间加一个等差数列或者询问一段区间至少要用多少个等差数列来表示.$n,m\leqslant 10^{5}$ 题解:老套路,维护差分数组,修改操作变成了两个单点加和 ...
- python实现tab键自动补全
一.查询python安装路径,一般默认是/usr/lib64/ [root@host2 ~]# python Python (r266:, Jul , ::) [GCC (Red Hat -)] on ...
- Java后缀数组-求sa数组
后缀数组的一些基本概念请自行百度,简单来说后缀数组就是一个字符串所有后缀大小排序后的一个集合,然后我们根据后缀数组的一些性质就可以实现各种需求. public class MySuffixArrayT ...