Hibernate profile HBM primary key generator selectable

Source: Internet
Author: User

Hibernate profile HBM primary key generator selectable1. Automatic Growth Identity

For MySQL, DB2, MS SQL Server, a database-generated primary key for generating unique identities for long, short, int types
Using the SQL Server and MySQL self-increment fields, this method cannot be put into Oracle, Oracle does not support self-increment fields, to set sequence (common in MySQL and SQL Server)
The syntax in the database 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 (primary) key, name varchar (20));

<id name= "id" column= "id" type= "long" >
<generator class= "Identity"/>
</id>

2, Sequence
DB2, Oracle-supported sequence for generating unique identities for long, short, or int
The syntax in the database is as follows:
Oracle:create sequence Seq_name increment by 1 start with 1;
You can call Seq_name.nextval or seq_name.curval when you need a primary key value, and the database will help us maintain this sequence sequence, guaranteeing that the value is unique each time it is taken, such as:
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 we do not specify the sequence parameter, hibernate accesses a default sequence, which is hibernate_sequence, and we also need to establish this sequence in the database.
In addition, sequence can also have another parameter is paramters, you can see the Hibernate API to understand its usage, see Org.hibernate.id.SequenceGenerator
Call the sequence of the database to generate the primary key, set the sequence name, otherwise hibernate cannot find:
<param name= "sequence" >NAME_SEQ</param> (common in Oracle)

3. Hilo
A long, short, or int type identifier generated using a high/low algorithm, given a table and a field as the source of the high value, the default table is Hibernate_unique_key, and the default field is Next_hi. It divides the source of the ID into two parts, db+ the memory, and then generates the ID value together by the algorithm, can produce many records in the few connection times, improves the 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>

Hibernate is responsible for generating low values when hibernate is persisted. The Hilo identifier generator needs to fetch the current value of Next_hi from the Hi_value table when generating the identifier, and then modify the value, which is done in a separate transaction. The maximum low value is configured in the property Max_lo, but when the low value generated in hibernate memory exceeds this value, it is necessary to read the high value again in the Hi_value table of the database.
Using the Hilo build policy, to create an additional table in the database, the default table name is Hibernate_unique_key, the default field is the integer type, and the name is Next_hi (less)
We can also set our own 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

Depending on the ability of the underlying database, choosing one from identity, sequence, Hilo, is more flexible, but at this point, if you choose sequence or Hilo, all the table's primary keys are taken from Hibernate's default sequence or Hilo table. Also, some databases are not very efficient to support the default primary key generation test
For Oracle in Sequence mode, for MySQL and SQL Server with identity (self-increment primary key generation mechanism), native is the primary key generation work to the database to complete, hibernate regardless (very common)

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

5, Seqhilo
The combination of sequence and Hilo, Hilo's high position is generated by sequence, so it also requires support from the underlying database
Implemented through the Hilo algorithm, but the primary key history is saved in Sequence and is applicable to databases that support Sequence, such as Oracle (less useful)

<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 generated by hibernate in the memory of the primary key, each increment of 1, not dependent on the underlying database, so all the database can be used, but the problem also comes, because it is hibernate generated, so only

Can have a hibernate application process to access the database, otherwise there will be a primary key conflict, can not be used in the cluster situation
Hibernate will add a self-incremented primary key to the primary key when inserting the data, but a hibernate instance maintains a counter, so this method cannot be used when multiple instances are running
<id name= "id" column= "id" >
<generator class= "Increment"/>
</id>

7.
Uuid.hex

Using a 128-bit UUID algorithm to generate the identifier of the string type, the UUID is encoded into a 32-bit 16-digit string. UUID contains: IP address, JVM start time, System time (up to 1/4 seconds), and a counter value (unique in JVM)
Hibernate calculates a 128-bit unique value to insert
<id name= "id" column= "id" >
<generator class= "Uuid.hex"/>
</id>

Uuid.string
Hibernate calculates a 16-bit value to insert

8, assigned
It is the responsibility of the application to generate primary key identifiers, often using a primary key that does not have a proxy primary key in the database, for business-related situations, such as:

<id name= "id" column= "id" type= "string" >
<generator class= "Assigned"/>
</id>

This primary key generation method is not recommended, the database table design should use the proxy primary key (surrogate key), should not use the natural primary key (natural key has business meaning), when the <generator> tag is not specified, The default is how the assigned primary key is generated
When inserting data, the primary key is added by the user, and hibernate does not care

9, Foreign
Use fields from external tables as primary keys

10. Select
Use triggers to generate primary keys (primarily for early database primary key generation mechanisms, less)

more about Hibernate : please click here
Hibernate : please click here.

Hibernate Chinese manual PDF http://www.linuxidc.com/Linux/2013-10/91208.htm

Hibernate profile HBM primary key generator selectable

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.