Hibernate primary key (generator) Generation mode

Source: Internet
Author: User
Tags db2 generator mysql in uuid

1) assigned

The primary key is generated by an external program, without hibernate involvement.

2) Hilo
The primary key generation mechanism implemented by the HI/LO algorithm requires additional database tables to save the primary key generation history state.

3) Seqhilo
Similar to Hilo, the primary key generation mechanism implemented by the HI/LO algorithm is simply that the primary key historical state is saved in sequence and is applicable to databases that support sequence, such as Oracle.

4) Increment
The primary key is incremented in numerical order. The implementation mechanism of this method is to maintain a variable in the current application instance to hold the current maximum value, and then add 1 to the primary key each time a primary key is generated. The possible problem with this approach is that if there are multiple instances accessing the same database, the different instances may generate the same primary key as each instance maintains the primary key state, causing the primary key to repeat the exception. Therefore, this approach must be avoided if there are multiple instance accesses to the same database.

5) identity
Adopt the primary key generation mechanism provided by the database. such as DB2, SQL Server, MySQL in the primary key generation mechanism.

6) Sequence
The primary key is generated using the sequence mechanism provided by the database. such as the sequence in Oralce.

7) native
Hibernate uses identity, Hilo, sequence as the primary key generation method according to the underlying database.

8) Uuid.hex
Hibernate generates a 16 binary value based on a 128-bit unique value generation algorithm (encoded with a string of length 32) as the primary key.

9) uuid.string
Similar to Uuid.hex, only the generated primary key is not encoded (length 16). Problems (such as PostgreSQL) may occur in some databases.

) Foreign
Use the field of the external table as the primary key.
In general, generating primary keys using the Uuid.hex approach provides the best performance and database platform adaptability.

        In addition, due to the common database, such as Oracle, DB2, SQL Server, MYSQL, etc., provides an easy-to-use primary key generation mechanism (auto-increase field or sequence). We can use Generator-class=native's primary key generation method on the primary key generation mechanism provided by the database. It is worth noting, however, that some databases provide a primary key generation mechanism that is not necessarily the most efficient, and that large amounts of concurrent insert data can cause interlocking between tables. The primary key generation mechanism provided by the database is often done by saving the current primary key state in an internal table (for example, the current maximum and increment is maintained in this internal table for the self-incrementing primary key), and then each time the data is inserted, the maximum value is read and the increment is used as the primary key for the new record. This new maximum value is then updated back to the internal table so that an insert operation can cause multiple table reads and writes inside the database, along with the locking unlock operation of the data, which has a significant impact on performance. Therefore, it is recommended to use Uuid.hex as the primary key generation mechanism for systems with high concurrent insert requirements.

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.