Hibernate 简明教程

Hibernate - Architecture

Hibernate 具有分层架构,它帮助用户在不知道底层 API 的情况下进行操作。Hibernate 利用数据库和配置数据向应用程序提供持久性服务(和持久性对象)。

Hibernate has a layered architecture which helps the user to operate without having to know the underlying APIs. Hibernate makes use of the database and configuration data to provide persistence services (and persistent objects) to the application.

以下是 Hibernate 应用程序架构的高级视图。

Following is a very high level view of the Hibernate Application Architecture.

hibernate high level

以下是对 Hibernate 应用程序架构及其重要核心类的详细视图。

Following is a detailed view of the Hibernate Application Architecture with its important core classes.

hibernate architecture

Hibernate 使用各种现有的 Java API,如 JDBC、Java 事务 API(JTA)、Java 命名和目录接口 (JNDI)。JDBC 提供对关系数据库通用功能的初级抽象,使几乎任何带有 JDBC 驱动的数据库都可以受 Hibernate 支持。JNDI 和 JTA 允许 Hibernate 与 J2EE 应用程序服务器集成。

Hibernate uses various existing Java APIs, like JDBC, Java Transaction API(JTA), and Java Naming and Directory Interface (JNDI). JDBC provides a rudimentary level of abstraction of functionality common to relational databases, allowing almost any database with a JDBC driver to be supported by Hibernate. JNDI and JTA allow Hibernate to be integrated with J2EE application servers.

以下部分简要描述了 Hibernate 应用程序架构中涉及的每个类对象。

Following section gives brief description of each of the class objects involved in Hibernate Application Architecture.

Configuration Object

Configuration 对象是您在任何 Hibernate 应用程序中创建的第一个 Hibernate 对象。它通常仅在应用程序初始化期间创建一次。它表示 Hibernate 所需的配置或属性文件。

The Configuration object is the first Hibernate object you create in any Hibernate application. It is usually created only once during application initialization. It represents a configuration or properties file required by the Hibernate.

Configuration 对象提供两个键组件:

The Configuration object provides two keys components −

  1. Database Connection − This is handled through one or more configuration files supported by Hibernate. These files are hibernate.properties and hibernate.cfg.xml.

  2. Class Mapping Setup − This component creates the connection between the Java classes and database tables.

SessionFactory Object

SessionFactory 对象使用提供的配置文件为应用程序配置 Hibernate,并允许实例化 Session 对象。SessionFactory 是一个线程安全对象,并且被应用程序的所有线程使用。

SessionFactory object configures Hibernate for the application using the supplied configuration file and allows for a Session object to be instantiated. The SessionFactory is a thread safe object and used by all the threads of an application.

SessionFactory 是一个重量级对象;它通常在应用程序启动期间创建并保留以便以后使用。您需要为使用独立配置文件的每个数据库创建一个 SessionFactory 对象。因此,如果您要使用多个数据库,则需要创建多个 SessionFactory 对象。

The SessionFactory is a heavyweight object; it is usually created during application start up and kept for later use. You would need one SessionFactory object per database using a separate configuration file. So, if you are using multiple databases, then you would have to create multiple SessionFactory objects.

Session Object

使用会话来与数据库获得物理连接。会话对象是轻量的,并且设计为在需要与数据库进行交互时进行实例化。持久性对象通过会话对象保存并检索。

A Session is used to get a physical connection with a database. The Session object is lightweight and designed to be instantiated each time an interaction is needed with the database. Persistent objects are saved and retrieved through a Session object.

会话对象不应长期保持打开状态,因为它们通常不是线程安全的,并且应根据需要创建并销毁它们。

The session objects should not be kept open for a long time because they are not usually thread safe and they should be created and destroyed them as needed.

Transaction Object

事务代表与数据库的工作单元,并且大多数 RDBMS 都支持事务功能。Hibernate 中的事务由底层事务管理器和事务(来自 JDBC 或 JTA)处理。

A Transaction represents a unit of work with the database and most of the RDBMS supports transaction functionality. Transactions in Hibernate are handled by an underlying transaction manager and transaction (from JDBC or JTA).

这是一个可选对象,Hibernate 应用程序可以选择不使用此接口,而是在其自己的应用程序代码中管理事务。

This is an optional object and Hibernate applications may choose not to use this interface, instead managing transactions in their own application code.

StandardServiceRegistry ssr = new StandardServiceRegistryBuilder().configure("hibernate.cfg.xml").build();
Metadata meta = new MetadataSources(ssr).getMetadataBuilder().build();

// Create the SessionFactory Instance
SessionFactory factory = meta.getSessionFactoryBuilder().build();

// Create the session
Session session = factory.openSession();

// Create the transaction
Transaction t = session.beginTransaction();

Query Object

查询对象使用 SQL 或 Hibernate 查询语言 (HQL) 字符串从数据库中检索数据并创建对象。Query 实例用于绑定查询参数,限制查询返回的结果数量,最终执行查询。

Query objects use SQL or Hibernate Query Language (HQL) string to retrieve data from the database and create objects. A Query instance is used to bind query parameters, limit the number of results returned by the query, and finally to execute the query.

Criteria Object

条件对象用于创建和执行面向对象的条件查询以检索对象。

Criteria objects are used to create and execute object oriented criteria queries to retrieve objects.