Hibernate 简明教程

Hibernate - ID 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:

  1. assigned − This generator indicates that the application will assign the primary key value. Hibernate doesn’t generate any value in this case.

  2. identity − This generator uses the database’s auto-increment feature to generate primary key values. It works with most databases and is suitable for simple use cases. Oracle does not support identity generator. MySQL, MS SQL Server, DB2, etc. supports this.

  3. sequence − This generator uses a database sequence to generate primary key values. It provides better performance and control compared to identity in some cases. Command to create a sequence create sequence <sequence_name> start with <number> increment by <number> Note − MySQL does not support sequences. Oracle does support sequences. Entry in employee.hbm.xml

<hibernate-mapping> <class name="com.mypackage.Employee" table="emp"> <id name="id"> <generator class=”sequence”> <param name=”sequence”>datasource_name</param> </generator> </id>…​</hibernate-mapping>

<hibernate-mapping> <class name="com.mypackage.Employee" table="emp"> <id name="id"> <generator class=”sequence”> <param name=”sequence”>datasource_name</param> </generator> </id> …​ </hibernate-mapping>

  1. increment − This generator generates primary key values by incrementing a value stored in memory.

  2. hilo − This generator uses a high-low algorithm to generate primary key values. It combines the advantages of sequence and increment.

  3. uuid − This generator generates universally unique identifiers (UUIDs) as primary key values. It is useful for distributed systems where unique IDs are required.

  4. native − This generator delegates the primary key generation strategy to the underlying database. If identity is supported by the underlying database, it chooses identity. Otherwise, it chooses sequence or hilo. It chooses the best strategy based on the database dialect.

  5. foreign − This generator uses the primary key value from another associated entity as the primary key value for the current entity.

Generating ID using Annotations

我们可以使用 IDENTITY 生成器注释来生成 ID 字段。请参见以下示例:

We can use IDENTITY generator annotation to generate the ID field. See the example below:

@Entity
public class Employee {
   @Id
   @GeneratedValue(strategy = GenerationType.IDENTITY)
   private Long id;

   // ... other fields
}