The main key generation strategies in Hibernate are as follows:
(1) UUID
The principle is to use the 128-bit UUID algorithm to generate the primary key, which ensures consistency in the network environment. When you use this build policy, the primary key of the entity class is of type string and is mapped into a table with a varchar field. Applies to all databases.
[Java]View Plaincopyprint?
- <id name="id" column="id" >
- <span style="White-space:pre" > </span><generator class="UUID" ></generator>
- </id>
(2) native
The principle is to automatically select sequence, identify, and Hilo based on the database type. When using this build strategy, the entity class's primary key is of type int, which is mapped into a table field int, and if MySQL, the ID is self-growing.
[Java]View Plaincopyprint?
- <id name="id" column="id" >
- <generator class="native" ></generator>
- </id>
(3) Identity
The principle is for MySQL, DB2, MS SQL Server, which uses a database-generated primary key to generate a unique identity for long, short, and int types.
(4) Increment
Used to generate a unique identity for a long, short, or int type. It can only be used if no other process is inserting data into the same table. Do not use under the cluster.
(5) Sequence
In Db2,postgresql, Oracle, SAP DB, Mckoi use sequences (sequence), and generators (generator) are used in InterBase. The returned identifiers are of type long, short, or int.
(6) Hilo
Use a high/low algorithm to efficiently generate identifiers for long, short, or int types. Given a table and a field (the default is Hibernate_unique_key and Next_hi, respectively) as the source of the high value. The high/low algorithm generates identifiers that are unique only in a particular database.
When using annotations, the default is: @GeneratedValue, equivalent to the native policy.
Hibernate primary key Generation policy