Hibernate primary key generation policy and hibernate primary key policy

Source: Internet
Author: User

Hibernate primary key generation policy and hibernate primary key policy

Hibernate Primary Key Generation Policy

1. Automatic growth of identity

Applicable to MySQL, DB2, and ms SQL Server. It uses the primary key generated by the database to generate unique identifiers 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 ));



2. sequence

Sequence supported by DB2 and Oracle, 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 ');



Seq_name

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:
NAME_SEQ (common in Oracle)

3. hilo

Long, short, or int type identifiers generated using 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, and the default field is next_hi. It divides the id source into two parts, DB + memory, and then generates the id value together according to 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 );



Hi_value
Next_hi
100

During hibernate Persistence, hibernate is responsible for generating low-level values. When the hilo identifier generator generates the identifier, it needs to retrieve the current value of next_hi from the hi_value table, and then modify the value. This operation is completed 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.






4. native

Select 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, the primary keys of all tables are retrieved from the default sequence or hilo table of Hibernate. 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 (very common)



5. seqhilo

With the combination of sequence and hilo, the high level of hilo is generated by sequence, so it also needs the support of 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)



Seq_name
100

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.


7,

Uuid. hex

A 128-bit UUID algorithm is used 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 a counter value (unique in JVM)
Hibernate calculates a 128-bit unique value insert.



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

8. assigned

The application is responsible for generating Primary Key Identifiers. The primary key is usually used in the database where there is no proxy primary key. The primary key used is business-related, such:



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 ), if no tag is 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)

Ps:

The proxy primary key is a record that is not related to the business and can uniquely identify the database. Generally, it is automatically generated by the database. For example, mysql can use auto_increment and Sql2000 can use identity generation, oracle can use sequence generation to generate a natural primary key, which is business-related. It is specified by the user and uniquely identifies any record in the database.

Brief version:

Increment: the proxy primary key. It is suitable for all databases. It is maintained by hibernate and auto-incrementing by the primary key. It has nothing to do with the underlying database, but it is not suitable for two or more hibernate processes.

Identity: proxy primary key, suitable for Mysql, ms SQL server, and other self-incrementing dbms. The primary key value cannot be maintained by hibernate.

Sequence: the proxy primary key, suitable for dbms supporting sequences such as oracle. The primary key value is not maintained by hibernate and is generated by sequences.

Native: proxy primary key. select an appropriate primary key generation policy based on the specific characteristics of the underlying database. If it is mysql or sqlserver, Select identity. If it is oracle, select sequence.

Hilo: the proxy primary key. hibernate uses the fields of a specific table as the hign value to generate the primary key value.

Uuid. hex: proxy primary key. hibernate uses the uuid 128-bit algorithm to generate a string-based primary key value.

Assign: A natural primary key that is suitable for application maintenance.

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.