Hibernate 简明教程

Hibernate - Mapping Files

对象/关系映射通常在 XML 文档中定义。此映射文件指示 Hibernate — 如何将定义的类或类映射到数据库表?

An Object/relational mappings are usually defined in an XML document. This mapping file instructs Hibernate — how to map the defined class or classes to the database tables?

尽管许多 Hibernate 用户选择手动编写 XML,但存在许多工具可用于生成映射文档。对于高级 Hibernate 用户,这些工具包括 XDoclet, MiddlegenAndroMDA

Though many Hibernate users choose to write the XML by hand, but a number of tools exist to generate the mapping document. These include XDoclet, Middlegen and AndroMDA for the advanced Hibernate users.

让我们考虑我们之前定义的 POJO 类,其对象将在下一部分定义的表中持久化。

Let us consider our previously defined POJO class whose objects will persist in the table defined in next section.

public class Employee {
   private int id;
   private String firstName;
   private String lastName;
   private int salary;

   public Employee() {}

   public Employee(String fname, String lname, int salary) {
      this.firstName = fname;
      this.lastName = lname;
      this.salary = salary;
   }

   public int getId() {
      return id;
   }

   public void setId( int id ) {
      this.id = id;
   }

   public String getFirstName() {
      return firstName;
   }

   public void setFirstName( String first_name ) {
      this.firstName = first_name;
   }

   public String getLastName() {
      return lastName;
   }

   public void setLastName( String last_name ) {
      this.lastName = last_name;
   }

   public int getSalary() {
      return salary;
   }

   public void setSalary( int salary ) {
      this.salary = salary;
   }
}

将有一个表对应于您愿意提供持久性的每个对象。考虑需要存储并检索到以下 RDBMS 表中的上述对象 −

There would be one table corresponding to each object you are willing to provide persistence. Consider above objects need to be stored and retrieved into the following RDBMS table −

create table EMPLOYEE (
   id INT NOT NULL auto_increment,
   first_name VARCHAR(20) default NULL,
   last_name  VARCHAR(20) default NULL,
   salary     INT  default NULL,
   PRIMARY KEY (id)
);

基于上述两个实体,我们可以定义以下映射文件,指示 Hibernate 如何将定义的类或类映射到数据库表。

Based on the two above entities, we can define following mapping file, which instructs Hibernate how to map the defined class or classes to the database tables.

<?xml version = "1.0" encoding = "utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">

<hibernate-mapping>
   <class name = "Employee" table = "EMPLOYEE">

      <meta attribute = "class-description">
         This class contains the employee detail.
      </meta>

      <id name = "id" type = "int" column = "id">
         <generator class="native"/>
      </id>

      <property name = "firstName" column = "first_name" type = "string"/>
      <property name = "lastName" column = "last_name" type = "string"/>
      <property name = "salary" column = "salary" type = "int"/>

   </class>
</hibernate-mapping>

您应该将映射文档保存在具有格式 <classname>.hbm.xml 的文件中。我们将映射文档保存在文件 Employee.hbm.xml 中。

You should save the mapping document in a file with the format <classname>.hbm.xml. We saved our mapping document in the file Employee.hbm.xml.

让我们详细了解映射文件 -

Let us see understand a little detail about the mapping elements used in the mapping file −

  1. The mapping document is an XML document having <hibernate-mapping> as the root element, which contains all the <class> elements.

  2. The <class> elements are used to define specific mappings from a Java classes to the database tables. The Java class name is specified using the name attribute of the class element and the database table name is specified using the table attribute.

  3. The <meta> element is optional element and can be used to create the class description.

  4. The <id> element maps the unique ID attribute in class to the primary key of the database table. The name attribute of the id element refers to the property in the class and the column attribute refers to the column in the database table. The type attribute holds the hibernate mapping type, this mapping types will convert from Java to SQL data type.

  5. The <generator> element within the id element is used to generate the primary key values automatically. The class attribute of the generator element is set to native to let hibernate pick up either identity, sequence, or hilo algorithm to create primary key depending upon the capabilities of the underlying database.

  6. The <property> element is used to map a Java class property to a column in the database table. The name attribute of the element refers to the property in the class and the column attribute refers to the column in the database table. The type attribute holds the hibernate mapping type, this mapping types will convert from Java to SQL data type.

还有其他可在映射文档中使用的属性和元素,在我讨论其他 Hibernate 相关主题时将尝试涵盖尽可能多的内容。

There are other attributes and elements available, which will be used in a mapping document and I would try to cover as many as possible while discussing other Hibernate related topics.

Generator

生成器类用于为对象生成一个 ID,该 ID 将成为数据库表的的主键。所有生成器类都实现 org.hibernate.id.IdentifierGenerator 接口。可以通过实现上述接口并重写 generator(SharedSessionContractImplementor sess, Object obj) 方法来创建自己的生成器类。

A generator class is used to generate an ID for the for an object, which is going to be the primary key of the database table. All generator classes implement org.hibernate.id.IdentifierGenerator interface. One can create their own generator class by implementing the above-mentioned interface and overriding the generator(SharedSessionContractImplementor sess, Object obj) method.

查看以下 employee.hbm.xml 文件片段:

Check the employee.hbm.xml file snippet below:

<hibernate-mapping>
   <class name="com.mypackage.Employee" table="emp">
      <id name="id">
         <generator class="assigned"></generator>
      </id>
...
</hibernate-mapping>

Types of Generator Classes

Hibernate 提供了许多预定义的 generator 类。Hibernate 中一些重要的预定义生成器类为:

Hibernate provides many predefined generator classes. Some of the important predefined generator classes in hibernate are: