Key generator primary key generator
Option description:
1) assigned
Primary Key from externalProgramGenerates data without hibernate.
2) HiLo
Use hi/LoAlgorithmThe 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. For exampleDB2, SQL Server, MySQLPrimary Key Generation Mechanism in.
6) sequence
Use the sequence mechanism provided by the database to generate the primary key.For example, sequence in oralce.
7) Native
HibernateUse identity, Hilo, and SequenceOne of them is used as the primary key generation method.
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 may not be optimal in terms of efficiency, and a large number of concurrent insert data may cause inter-Table locks. 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 is read each time data is inserted, and the incremental value is added as the primary key of the new record, then, the new maximum value is updated back to the internal table. In this way, an insert operation may result in multiple internal table read/write operations in the database, along with data lock and unlock operations, this 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.