Generate a key generator from a hibernate primary key
The Hibernate primary key generator is used to generate the primary key for data table records. There are several common primary key generation methods.
Key Generator
Primary key generator:
First, we will introduce several common primary key generators:
1) Increment
Increment: generates an auto-increment primary key for long, short, or Int data columns.
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
For example, this method must be avoided. 2) Identity
For databases that support auto-increment columns, such as SQL Server and MySQL, if the data column type is long, short or Int, you can use the primary key generator to generate auto-increment primary keys. 3) seqhilo
For databases that support sequence, such as Oracle and DB2, if the data column type is long, short or Int, you can use this primary key generator to generate an Automatically increasing primary key. For more information about sequence, see
Http://easyworld.javaeye.com/blog/214098
4) UUID:
Use the 128-bit UUID algorithm to generate a unique string primary key for string column data. 5) HiLo
The primary key generation mechanism implemented by the HI/lo algorithm requires additional database tables to save the Historical Status of the primary key generation. 6) Foreign
Use the field of the External table as the primary key. 7) Native
Hibernate uses one of identity, Hilo, and sequence as the primary key generation method based on the underlying database. This method is often used during the process, this means that the primary key generation method is determined by the underlying database. 8) assigned
The primary key is generated by an external program and does not involve hibernate. 9) 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. 10) UUID. String
Similar to UUID. HEX, the generated primary key is not encoded (Length: 16 ). Problems may occur in some databases (such as PostgreSQL ). 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, UUID. Hex is recommended for systems with high requirements for concurrent insert.
As the primary key generation mechanism. 1. Increment identifier generator the generator is assigned a value for the proxy primary key in incremental mode by hibernate. In the initialization phase, Hibernate reads the maximum primary key value in the table. When a record is inserted, the value increases progressively based on the maximum value, and the increment is 1. If two hibernate application processes access the same database table, the same maximum value may be obtained at the same time, resulting in the same primary key value. Thus, insertion of a process fails! Scope of use:
-The identifier generation mechanism does not depend on the underlying database system, so it is suitable for all database systems.
-This method is applicable when only a single hibernate application process accesses the same database. It is not recommended in a cluster environment.
-The OID must be of the long, Int, or short type. If it is defined as a byte type, an exception is thrown. 2. Identity identifier generator this generator is used by the underlying database to generate identifiers. It requires the underlying database to define the primary key as an Automatically increasing field type. Applicability:-because it depends on the underlying database, the underlying data system must support automatic field growth. Including: DB2, MySQL, MSSQL, Sybase, HSQLDB, Informix, etc.
-The OID must be of the long, Int, or short type. If it is defined as a byte type, an exception is thrown. 3. Sequence identifier generator This identifier generator uses the sequence provided by the underlying database to generate the identifier.
<Generator class = "sequence">
<Param name = "sequence"> tester_id_seq </param>
</Generator>
The following DDL code is generated:
Create sequence tester_id_seq;
Note: MySQL does not support sequence. When hibernate persists a sequencetester object, it first obtains a unique serial number from the tester_id_seq sequence of the underlying database, and then uses it as the primary key value. Applicability:-the underlying database must support sequences, including Oracle, DB2, sap db, and PostgreSQL.
-The OID must be of the long, Int, or short type. If it is defined as a byte type, an exception is thrown. 4. the Hilo identifier generator HiLo identifier generator is generated by hibernate based on a high/low algorithm. It obtains the high value from a specific table field in the database.
<ID name = "ID" type = "long" column = "ID">
<Generator class = "HiLo">
<Param name = "table"> hi_value </param>
<Param name = "column"> next_value </param>
<Param name = "max_lo"> 100 </param>
</Generator>
</ID>
Example above: place the high value in the next_value field of the hi_value table
When hibernate persists an object, it needs to read and modify the next_value value in the hi_value table. This operation is processed in a separate transaction. When saving, a new transaction is created in a new database connection instead of using the current database connection and transaction of the current session object, and then the hi_value table is accessed. Applicability:-applicable to all database systems.
-The OID must be of the long, Int, or short type. If it is defined as a byte type, an exception is thrown.
-Only one database can have unique identifiers.
-HiLo is not applicable when you provide a database connection for hibernate, Or hibernate obtains a database connection from the data source of the application server through JTA, as a result, Hilo cannot access the hi_value table in the transaction of the new database connection. In this case, seqhilo generators can be applied if the database system supports sequences. Seqhilo can be applied to database systems that support sequences. It obtains high values from sequences. 5. Native identifier generator this generator selects the applicable identity, sequence, or HiLo identifier generator based on the ability of the underlying database to automatically generate identifiers. It can automatically determine the identifier generation mechanism provided by the underlying database. Applicability:-suitable for cross-database platform development, that is, when the same hibernate application needs to connect to multiple Database Systems
-The OID must be of the long, Int, or short type. If it is defined as a byte type, an exception is thrown.