In hibernate, the problem of ID generator in MySQL 5.0 is resolved from the Internet.

Source: Internet
Author: User

Key Generator
1) assigned
Primary Key from external Program Generates data without hibernate.
2) HiLo
Use hi/Lo Algorithm The implementation of the primary key generation mechanism requires additional database tables to save the Historical Status of the primary key generation.
3) seqhilo
Similar to hilo, the primary key generation mechanism implemented through the HI/lo algorithm only stores the Historical Status of the primary key in sequence, which is suitable for databases that support sequence, such as oracle.
4) Increment
The primary key increments in numerical order. The implementation mechanism of this method is to maintain a variable in the current application instance to save the current maximum value, and then add 1 as the primary key each time the primary key needs to be generated.
This method may cause a problem: if multiple instances access the same database, different instances may generate the same primary key because each instance maintains its primary key status, this causes duplicate primary key exceptions. Therefore, if the same database has multiple instances to access, this method must be avoided.
5) Identity
Use the primary key generation mechanism provided by the database. Such as the primary key generation mechanism in DB2, SQL Server, and MySQL.
6) sequence
Use the sequence mechanism provided by the database to generate the primary key. For example, sequence in oralce.
7) Native
Hibernate uses identity, Hilo, and sequence as the primary key generation method based on the underlying database.
8) UUID. HEX
The algorithm generated by hibernate Based on the 128-bit unique value generates a hexadecimal value (encoded with a 32-Bit String) as the primary key.
9) UUID. String
Similar to UUID. HEX, the generated primary key is not encoded (Length: 16 ). Problems may occur in some databases (such as PostgreSQL ).
10) Foreign
Use the field of the External table as the primary key. Generally, using UUID. HEX to generate a primary key provides the best performance and database platform adaptability.
In addition, common databases such as Oracle, DB2, SQL Server, and MySQL provide easy-to-use primary key generation mechanisms (auto-increase fields or sequence ). We can use the primary key generation method of generator-class = native in the primary key generation mechanism provided by the database. However, it is worth noting that the primary key generation mechanism provided by some databases is not necessarily the best in terms of efficiency,
A large number of concurrent insert data may cause mutual locks between tables. The primary key generation mechanism provided by the database is usually to save the current primary key status in an internal table (for example, for an auto-incrementing primary key, this internal table maintains the current maximum and incremental values ),
Then, the maximum value will be read each time the data is inserted, and the incremental value will be added as the primary key of the new record. Then, the new maximum value will be updated back to the internal table, an insert operation may result in multiple table read/write operations in the database, along with data locking and unlocking operations, which has a great impact on performance.
Therefore, for systems with high requirements for concurrent insert, UUID. Hex is recommended as the primary key generation mechanism.
3. If you need to use a custom primary key generation algorithm, configure the primary key generator here. The primary key generator must implement the net. SF. hibernate. Id. identifiergenerator interface.

Related Article

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.