1) assigned
The primary key is generated by an external program and does not involve hibernate.
2) HiLo
The primary key generation mechanism implemented by the HI/lo algorithm requires additional database tables to store the primary key
Key Generation history status.
3) seqhilo
Similar to hilo, the primary key generation mechanism implemented through the HI/LO algorithm is only the primary key history.
The status is stored in sequence, which is applicable to databases that support sequence, such as oracle.
4) Increment
The primary key increments in numerical order. The implementation mechanism of this method is maintained in the current application instance.
A variable to save the current maximum value, and each time the primary key needs to be generated
Add 1 as the primary key.
This method may cause a problem: if multiple instances currently access the same data
Because each instance maintains its primary key status, different instances may generate the same
As a result, duplicate primary keys are abnormal. Therefore, if the same database has multiple instances
For example, this method must be avoided.
5) Identity
Use the primary key generation mechanism provided by the database. Such as DB2, SQL Server, MySQL
Primary Key Generation Mechanism in.
6) sequence
Use the sequence mechanism provided by the database to generate the primary key. For example
Sequence.
7) Native
Hibernate determines the use of identity, Hilo, and Sequence Based on the underlying database.
One 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 (after Encoding
Is expressed as a string of 32 characters) as the primary key.
9) UUID. String
Similar to UUID. HEX, the generated primary key is not encoded (Length: 16 ). In some
Problems may occur in the database (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 suitability.
Response.
In addition, common databases such as Oracle, DB2, SQL Server, and MySQL All provide
It provides an easy-to-use primary key generation mechanism (auto-increase field or sequence ). We can
The primary key generation mechanism provided by the database adopts the primary key generation method of generator-class = native.
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 usually stores the current primary key in an internal table
(For example, for an auto-increment primary key, this internal table maintains the current maximum value and incremental amount ),
Then, the maximum value is read each time the data is inserted, and the incremental value is added as the primary key of the new record.
Then update the new maximum value back to the internal table. In this way, an insert operation may cause data
Multiple table read/write operations in the database, along with data lock and unlock operations, which results in performance
.
Therefore, for systems with high requirements for concurrent insert, UUID. Hex is recommended for primary key generation.
Mechanism.
Create or replace trigger tib_report
Before insert
On sms_report
For each row
Declare integrity_error exception; errno integer; errmsg char (200); dummy integer; found Boolean;
Begin
-- Column "ID" uses sequence booking_seq
Select sms_report_seq.nextval into: New. rpt_id from dual;
-- Errors handling
Exception
When
Integrity_error then raise_application_error (errno, errmsg); end;