Increment
Generates an auto-grow primary key for a long, int, short data column. Used in the database to not set the table primary key to self-increment, but also want the table primary key self-increment
Implementation mechanism: Maintain a variable in the current application instance to hold the current maximum value, then add 1 to the primary key each time you need to generate a primary key
Scope of Use: if there are multiple instances accessing the same database, because each instance maintains the primary key state, different instances may generate the same primary key, causing the primary key to repeat the exception. Therefore, this approach must be avoided if there are multiple instances of the same database (that is, a large number of concurrent) questions. Use only if no other process is inserting data into the same table (not used under the cluster). But its generating identifier mechanism does not depend on the underlying database system, so he is suitable for all database systems
Identity
For databases that support autogrow columns, such as Sqlserver,mysql, you can use this primary key generator to generate auto-grow primary keys if the type of the data column is long, short, or int
Scope of use: requires that the underlying data system must support autogrow field types. And it requires the underlying database to define the primary key as the autogrow field type
native
Choose to use identity, sequence, or Hilo identity Fuzhou Builder, depending on the underlying database's ability to automatically generate identifiers. mechanism to automatically determine the generated identifiers provided by the underlying database
Scope of use: Suitable for cross-database platform development, that is, the same hibernate application needs to connect to a variety of database systems
sequence
For databases that support sequence such as ORACLE,DB2, you can use this primary key generator to generate an autogrow primary key if the type of the data column is long,short or int. The identifier generator uses the sequence provided by the underlying database to generate identifiers
<id name= "id" column= "id" > <generator class= "sequence" > <!--Create series in database Usertb_seq <param name= "sequence" >userTb_seq</param> </generator></id>
Uuid.hex
Generated by hibernate based on a 128-bit unique value generation algorithm 16 binary values (encoded with a string of length 32), as the primary key
uuid.string
Similar to Uuid.hex, only the generated primary key is not encoded (length 16). Problems may occur in some databases (such as PostgreSQL),
assigned
The primary key is generated by an external program
<id name= "id" column= "id" > <generator class= "Assigned"/></id>
Hilo
The identifier is generated by hibernate in accordance with a high/low algorithm, to create an additional table in the database, default table name (Hibernate_unque_key), Default field (Next_hi:integer type) ; He gets 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>
Above example: High values are placed in the Next_value field of the Hi_value table
Hibernate when you persist an object, you need to read and modify the Next_value value in the Hi_value table. This operation is handled in a separate transaction. When save, instead of using the current database join and transaction for the current session object, create a new transaction in a new database connection and then access the Hi_value table.
Scope of application:
Applies to all database systems.
The OID must be a long, int, or short type, and if defined as a byte type, an exception is thrown.
Identifiers can only be guaranteed in one database
Hilo is not available when a user provides a database join for Hibernate itself, or hibernate passes JTA to obtain a database join from the application server's data source, because it does not guarantee that Hilo will access the Hi_value table in the transaction of the new database connection. In this case, the Seqhilo generator is applicable if the database system supports sequences. For a database system that supports sequences, Seqhilo is used, which gets the high value from the sequence.
In general, generating primary keys using the Uuid.hex approach provides the best performance and database platform adaptability.
In addition, due to the common database, such as Oracle, DB2, SQL Server, MYSQL, etc., all provide an easy-to-use primary key generation mechanism (auto-increase field or sequence). The primary key generation mechanism of generator-class=native can be used to generate the primary key generated by the database. It is worth noting, however, that some databases provide a primary key generation mechanism that is not necessarily the most efficient, and that large amounts of concurrent insert data can cause interlocking between 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, the current maximum and increment is maintained in this internal table for the self-incrementing primary key), and then each time the data is inserted, the maximum value is read and the increment is used as the primary key for the new record. This new maximum value is then updated back to the internal table so that an insert operation can cause multiple table reads and writes inside the database, along with the locking unlock operation of the data, which has a significant impact on performance. Therefore, it is recommended to use Uuid.hex as the primary key generation mechanism for systems with high concurrent insert requirements.
Foregin
Indicates that the identity property value of the object directly using another association (that is, the persisted object cannot generate a primary key); This primary key generator is only useful in 1-1 Association mappings based on the primary key
Select
Selects a row of a unique primary key from a database trigger and returns its primary key value as the IDENTITY property value
Custom PRIMARY Key Generator
The primary key generator must implement the Net.sf.hibernate.id.IdentifierGenerator interface
Primary key Generation class Myidentifiergenerator
public class Myincrementgenerator implements Identifiergenerator, configurable{private static final log log = Logfactory.getlog (Myincrementgenerator.class); Private long next; private String SQL; Public synchronized Serializable Generate (Sessionimplementor session, Object object) throws SQLException, Hibe rnateexception {if (sql!=null) {///Get the next primary key number, you can define the GetNext (Session.connection ()); } return string.valueof (next); } public void Configure (type type, Properties params, dialect D) throws mappingexception{String table = params . GetProperty ("table"); if (table==null) Table = Params.getproperty (persistentidentifiergenerator.table); String column = Params.getproperty ("column"); if (column==null) column = Params.getproperty (persistentidentifiergenerator.pk); String schema = Params.getproperty (PersistentidentifiergeneraTor. SCHEMA); Returnclass = Type.getreturnedclass (); sql = "Select Max (To_number (" + column + ")) from" + (Schema==null? Table:schema + '. ' + table); } private void GetNext (Connection conn) throws SQLException {PreparedStatement st = conn.preparestatement (SQL) ; ResultSet rs = null; try {rs = St.executequery (); if (Rs.next ()) {next = Rs.getlong (1) + 1; if (Rs.wasnull ()) next = 1; } else {next = 1; } sql=null; Log.debug ("First free ID:" + next); } finally {if (rs!=null) rs.close (); St.close (); } }
You then need to use the custom primary key generator to construct the primary key for the database object that corresponds to the. The XML file can be written like this:
<id name= "UniqueID" column= "UniqueID" type= "string" > <generator class= " Com.core.persistence.MyIncrementGenerator "/></id>
Hibernate-----Hibernate primary Key Generator