Native Hibernate与Hibernate JPA
Jpa是java持久化规范,而hibernate可以看做是jpa的一个实现,hibernate框架的的使用分为两种方式,"原生Hibernate"和"Jpa Hibernate"。以下是我转载的一篇翻译。
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.
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.
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.
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.
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则需要额外的工作。