Hibernate, as an ORM solution, effectively "sits between" the Java application data access layer and the Relational Database, as can be seen in the diagram above. The Java application makes use of the Hibernate APIs to load, store, query, etc its domain data. Here we will introduce the essential Hibernate APIs. This will be a brief introduction; we will discuss these contracts in detail later./div>
Hibernate,作為ORM ( Object-Relational Mapping,將關聯式資料庫映射至物件導向的資料抽象化技術),能有效地設立在將Java應用程式數據訪問層和關連式資料庫之間。如圖片所示,Java應用程式可以利用Hibernate API來下載、儲存、查詢等行為於自身的數據域。
As a JPA provider, Hibernate implements the Java Persistence API specifications and the association between JPA interfaces and Hibernate specific implementations can be visualized in the following diagram:
作為一個JPA(Java Persistence API,Java持久化應用程式接口;API:Application Programming Interface)提供者,Hibernate實現JPA的規範,並提供JPA界面的接口,兩者關係可以透過下方圖中看到:
- SessionFactory (
org.hibernate.SessionFactory
) - A thread-safe (and immutable) representation of the mapping of the application domain model to a database. Acts as a factory for
org.hibernate.Session
instances. TheEntityManagerFactory
is the JPA equivalent of aSessionFactory
and basically those two converge into the sameSessionFactory
implementation.ASessionFactory
is very expensive to create, so, for any given database, the application should have only one associatedSessionFactory
. TheSessionFactory
maintains services that Hibernate uses across allSession(s)
such as second level caches, connection pools, transaction system integrations, etc. - 一個表示應用領域模型映射到資料庫的線程安全(及不可變的)。為運作 org.hibernate.Session 的實例工廠。 而EntityManagerFactory相當於SessionFactory 的APJ(Java Persistence API,Java持久化應用程式接口;API:Application Programming Interface),基本上這兩個會匯集成同樣的
SessionFactory
實作。
- SessionFactory創建成本非常昂貴,所以對任何給予的資料庫,應用程序應該只有一個SessionFactory。SessionFactory的Hibernate使用所有Session(s),像是 二級緩存、連接池、交易系統集成等等的 維護服務。
- Session (
org.hibernate.Session
) - A single-threaded, short-lived object conceptually modeling a "Unit of Work" PoEAA. In JPA nomenclature, the
Session
is represented by anEntityManager
.Behind the scenes, the HibernateSession
wraps a JDBCjava.sql.Connection
and acts as a factory fororg.hibernate.Transaction
instances. It maintains a generally "repeatable read" persistence context (first level cache) of the application domain model. - 是一個單線程、生命短暫的物件去概念地模型出一個“工作單位”企業應用程式架構模式(PoEAA,Patterns of Enterprise Application Architecture)。在JPA(Java持久化應用程式接口:Java Persistence API;API:Application Programming Interface)的命名中,Session 被 代表為一個 EntityManager。
- 在幕後,Hibernate Session 包裝一個 JDBC java.sql.Connection 並 運作像一個 org.hibernate.Transaction 的實例工廠,他可以維護一般地 “可重復性閱讀”的應用程序域模型中的持久性脈絡(一級緩存)。
- Transaction (
org.hibernate.Transaction
) - A single-threaded, short-lived object used by the application to demarcate individual physical transaction boundaries.
EntityTransaction
is the JPA equivalent and both act as an abstraction API to isolate the application from the underlying transaction system in use (JDBC or JTA). - 一個單線程、生命短暫的物件被使用在應用程式去劃定出個別的物理交易界線。 EntityTransaction是 JPA 的相等物,且兩者都作為一個抽象API去隔離使用中的基礎交易系統(JDBC or JTA)。