Hiberbate Primary Key Generation Policy

Source: Internet
Author: User

Let's take a look at several common primary key generation methods:
1,

Automatic growth identity: Applicable to MySQL, DB2, and ms SQL Server. It uses the primary key generated by the database to generate a unique ID for long, short, and INT types.
Use auto-increment fields of SQL Server and MySQL. This method cannot be placed in Oracle. Oracle does not support auto-increment fields. You need to set sequence (it is often used in MySQL and SQL Server)
The database syntax is as follows:
MySQL: Create Table t_user (ID int auto_increment primary key, name varchar (20 ));
SQL Server: Create Table t_user (ID int identity (1, 1) primary key, name varchar (20 ));

<ID name = "ID" column = "ID" type = "long">
<Generator class = "Identity"/>
</ID>
 
2,

Sequence: a sequence supported by DB2 and Oracle. It is used to generate a unique identifier for long, short, or Int.
The database syntax is as follows:
ORACLE: Create sequence seq_name increment by 1 start with 1;
When you need a primary key value, you can call seq_name.nextval or seq_name.curval to obtain it. The database will help us maintain this sequence and ensure that each retrieved value is unique, for example:
Insert into tbl_name (ID, name) values (seq_name.nextval, 'jimliu ');

<ID name = "ID" column = "ID" type = "long">
<Generator class = "sequence">
<Param name = "sequence"> seq_name </param>
</Generator>
</ID>
 
If the sequence parameter is not specified, Hibernate will access a default sequence, which is hibernate_sequence. We also need to create this sequence in the database.
In addition, sequence can have another parameter paramters. You can view the hibernate API to understand its usage. For details, see org. hibernate. Id. sequencegenerator.
Call the database sequence to generate the primary key. You need to set the sequence name. Otherwise, Hibernate cannot find it:
<Param name = "sequence"> name_seq </param> (common in Oracle)

3,

HiLo: long, short, or Int type identifier generated by a high/low level algorithm. A table and field are given as the source of the high value. The default table is hibernate_unique_key, the default field is next_hi. It sets

The product source is divided into two parts, DB + memory, and then the id value is generated by combining the algorithm, multiple records can be generated within a few connections, improving efficiency
MySQL: create table hi_value (next_hi integer not null );

Insert into hi_value (next_hi) values (1 );

<Id name = "id" column = "id">
<Generator class = "hilo">
<Param name = "table"> hi_value </param>
<Param name = "column"> next_hi </param>
<Param name = "max_lo"> 100 </param>
</Generator>
</Id>
 
During hibernate Persistence, hibernate is responsible for generating low-level values. The hilo identifier generator needs to retrieve the current value of next_hi from the hi_value table when generating the identifier, and then modify the value. This operation is performed in a separate transaction.

. The maximum low value is configured in the max_lo attribute, but when the low value generated in the Hibernate memory exceeds this value, you need to read the high value again in the hi_value table of the database.
To use the Hilo generation policy, create an additional table in the database. The default table name is hibernate_unique_key. The default field is of the integer type and the name is next_hi (rarely used)
You can also set the custom table name and field name.
<ID name = "ID" type = "integer">
<Column name = "ID"/>
<Generator class = "HiLo">
<Param name = "my_unique_key"/>
<Param column = "next_hi"/>
</Generator>
</ID>

4,

Native: selects one from identity, sequence, and HiLo Based on the capabilities of the underlying database, which is more flexible. However, if sequence or HiLo is selected, then, the primary keys of all tables are replaced by the default values

Sequence or HiLo table. In addition, some databases support the test of primary key generation by default, and the efficiency is not very high.
For Oracle using sequence, For MySQL and SQL Server using identity (auto-incrementing primary key generation mechanism), native is to submit the primary key generation work to the database, Hibernate (usually

Use)

<ID name = "ID" column = "ID">
<Generator class = "native"/>
</Id>
 
5,

Seqhilo: the combination of sequence and hilo. The high level of hilo is generated by sequence, so it also requires support from the underlying database.
Implemented through the hilo algorithm, but the primary key history is stored in Sequence. It is suitable for databases that support Sequence, such as Oracle (rarely used)

<Id name = "id" column = "id">
<Generator class = "seqhilo">
<Param name = "sequence"> seq_name </param>
<Param name = "max_lo"> 100 </param>
</Generator>
</Id>
 
6,

Increment: This is the primary key generated by Hibernate in the memory. Each increment is 1 and does not depend on the underlying database. Therefore, all databases can be used, but the problem also arises, because it is generated by Hibernate, only

A Hibernate application process can access the database. Otherwise, a primary key conflict occurs and cannot be used in a cluster.
Hibernate adds an auto-incrementing primary key to the primary key when inserting data, but a hibernate instance maintains a counter. Therefore, this method cannot be used when multiple instances are running.

<Id name = "id" column = "id">
<Generator class = "increment"/>
</Id>
 
7,

Uuid. hex: uses a 128-bit UUID algorithm to generate a string-type identifier. UUID is encoded into a 32-bit hexadecimal number string. UUID includes: IP address, JVM start time, system time (accurate to 1/4 seconds) and

Counter value (unique in JVM)
Hibernate calculates a 128-bit unique value insert.

<Id name = "id" column = "id">
<Generator class = "uuid. hex"/>
</Id>

Uuid. string
Hibernate calculates a 16-Bit Insert value.

8,

Assigned: The primary key identifier generated by the application. It is usually used in the database without a proxy primary key. The primary key used is related to the business, for example:

<Id name = "id" column = "id" type = "string">
<Generator class = "assigned"/>
</Id>
 
This primary key generation method is not recommended. When designing a database table, you should use a proxy primary key (surrogate key) instead of a natural primary key (natural key has business meaning ), when the <generator> label is not specified,

The default mode is assigned primary key generation.
When inserting data, the primary key is added by the user, and hibernate does not

9,
Foreign
Use External table fields as primary keys

10,
Select

Use a trigger to generate a primary key (mainly used to generate a primary key for an earlier database, with less use)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.