天天看點

原生Hibernate和Jpa Hibernate

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.

我知道,使用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則需要額外的工作。

本文作者:蘇生米沿

本文轉載自:http://blog.csdn.net/sushengmiyan/article/details/50182005

翻譯來源:http://stackoverflow.com/questions/20820880/hibernate-native-vs-hibernate-jpa