Hibernate primary key generation method key generator
Primary key Generator
Optional Description:
1) assigned
The primary key is generated by the external program, without hibernate participation.
2) Hilo
The primary key generation mechanism implemented by HI/LO algorithm requires additional database tables to save the primary key generation history state.
3) Seqhilo
Similar to the Hilo, the primary key generation mechanism implemented through the HI/LO algorithm is that the primary key history state is stored in sequence and is suitable for databases supporting sequence, such as Oracle.
4) Increment
The primary keys are 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 each time a primary key is needed
Add this value to 1 as the primary key.
The problem with this approach may be that if more than one instance is currently accessing the same database, then because each instance maintains a primary key state, the same primary key may be generated by different instances, causing the primary key to repeat the exception. Therefore, this method must be avoided if multiple instances of the same database are accessed.
5) identity
Using the primary key generation mechanism provided by the database. such as DB2, SQL Server, the primary key generation mechanism in MySQL.
6) Sequence
The primary key is generated using the sequence mechanism provided by the database. such as the sequence in Oralce.
7) native
By hibernate according to the underlying database, identity, Hilo and sequence are adopted as primary key generation methods.
8) Uuid.hex
The hibernate based on the 128-bit unique value generation algorithm generates a 16 binary value (encoded as 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 fields of the external table as the primary key.
In general, using Uuid.hex to generate primary keys provides the best performance and database platform adaptability.
In addition, the commonly used databases, such as Oracle, DB2, SQL Server, MYSQL, and so on, provide an Easy-to-use primary key generation mechanism (auto-increase field or sequence). The primary key generation mechanism provided by the database can be used to generate the generator-class=native of primary keys. However, it is worth noting that some databases provide primary key generation mechanism is not necessarily the best efficiency, a large number of concurrent insert data may cause the interlock between the 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, for a self-added primary key). The current maximum and increment are maintained in this internal table, and the maximum value is read each time the data is inserted, then the increment is added as the primary key for the new record. The new maximum value is then updated back into the internal table, so that an insert operation can result in multiple table read and write operations inside the database, along with the lock of the data, which has a significant effect on performance.
Therefore, it is recommended to adopt Uuid.hex as the primary key generation mechanism for systems with high concurrent insert requirements.