Hibernate primary key generation method and Configuration

Source: Internet
Author: User
Tags interbase

Key Generator primary Key Generator

<Generator> the element is used to set the identifier generator. Hibernate provides the identifier Generator interface org. hibernate. id. IdentifierGenerator, and provides multiple built-in implementations. For example, org. hibernate. id. IdentityGenerator; org. hibernate. id. IncrementGenerator; they are abbreviated as identity and increment. When setting the class attribute of the <generator> sub-element, You can provide both the complete class Name of the identifier generator and the abbreviated name.

Identifier Generator

DescriptionMing

Increment

Applies to the proxy primary key. ByHibernateThe identifier is automatically generated in ascending order.1.
Advantage: because its mechanism does not depend on the underlying database system, it is suitable for all database systems.
Disadvantage: Only applicable to a singleHibernateIt is not recommended that application processes access the same database in a cluster environment. In addition, OIDMust beLong, int, shortType.ByteType.

Identity

Applies to the proxy primary key.The identifier generated by the underlying database. The precondition is that the underlying database supports automatic growth of field types.(OracleThe database cannot use it.)

Sequence

Applies to the proxy primary key.HibernateGenerates identifiers Based on the sequence of the underlying database. The prerequisite is that the underlying database supports sequences.(OracleThe database can use it.)

Hilo 

Applies to the proxy primary key.HibernateAccordingHigh/lowAlgorithm to generate an identifier.HibernateUse the fields of a specific table"High"Value.Optional by defaultHibernate_unique_keyTableNext_hiField. Its mechanism does not depend on the underlying database system, so it is suitable for all database systems.High/lowThe identifier generated by the algorithm must be unique in only one database.

Native

Applicable to proxy primary keys. Based on the ability of the underlying database to automatically generate identifiers.Identity,Sequence,Hilo.Suitable for cross-platform development, That is, the sameHibernateApplications need to connect to multiple database systems.

Uuid. hex

Applies to the proxy primary key.HibernateUse128BitUUIDAlgorithm to generate an identifier.UUIDThe algorithm can generate a unique string identifier in the network environment. This type of identifier generation policy is not popular, because string-type primary keys occupy more database space than integer-type primary keys.

Assigned

Applicable to natural primary keys. ByJavaThe application is responsible for generating identifiers.JavaApplication settingsOID,You cannotSetID ()The method declaration isPrivateType,Avoid using a natural primary key whenever possible.

 

 

Option description:
1)Assigned
The primary key is generated by an external program.HibernateParticipate.

 

Hilo
PassHi/loThe primary key generation mechanism implemented by the algorithm requires additional database tables to save the Historical Status of the primary key generation.

 

Seqhilo
AndHiloSimilarlyHi/loThe algorithm implements the primary key generation mechanism, but the Historical Status of the primary key is saved inSequenceMedium, applicable to supportSequenceSuchOracle.


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 this value every time the primary key needs to be generated1As the primary key. 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.

 

Identity
Use the primary key generation mechanism provided by the database. For exampleDB2,SQL Server,MySQLPrimary Key Generation Mechanism in.

 

Sequence
  UseSequenceMechanism to generate a primary key. For exampleOralceInSequence.

 

Native
ByHibernateUse it based on the underlying databaseIdentity,Hilo,SequenceOne of them is used as the primary key generation method.


 


8)

 

Uuid. hex
ByHibernateBased on128Bit unique value generation algorithm generation16Hexadecimal value (encoded with length)32As the primary key.

 

Uuid. string
AndUuid. hexSimilarly, only the generated primary key is not encoded (Length16). Problems may occur in some databases (suchPostgreSQL).

 

Foreign
Use the field of the External table as the primary key.
GenerallyUuid. hexGeneration of primary keys will provide the best performance and database platform adaptability.
In addition, due to common databases, suchOracle,DB2,SQLServer,MySqlAnd provides an easy-to-use primary key generation mechanism (Auto-IncreaseField orSequence). We can use the primary key generation mechanism provided by the databaseGenerator-class = nativePrimary Key Generation Method.

 

However, it is worth noting that the primary key generation mechanism provided by some databases is not necessarily the best in efficiency, and a large number of concurrentInsertData may cause mutual locks between tables. 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 update the new maximum value to the internal table.InsertOperations may result in multiple internal table read/write operations in the database, along with data locking and unlocking operations, which has a great impact on performance. Therefore, for concurrencyInsertSystems with high requirements are recommendedUuid. hexAs the primary key generation mechanism.

 

Increment: generate a long, short, or int type primary key, which cannot be used in the cluster environment. Applicable to All databases
Identity: generate a long, short, or int type primary key. For DB2, MySQL, ms SQL Server, Sybase and HypersonicSQL
Sequence: generate a long, short, or int type primary key. Applicable to DB2, PostgreSQL, Oracle, sap db, McKoi, and Interbase.
Hilo: generate a long, short, or int type primary key. A database table is required to store the generated primary key information. Do not use this primary key generation method when using the database connection or custom database connection provided by the application server's JTA. Applicable to All databases
Seqhilo: uses the sequence of the given database to generate a long, short, or int type primary key. Applicable to DB2, PostgreSQL, Oracle, sap db, McKoi, and Interbase.
Uuid. hex: uses the 128-bit algorithm to generate a 32-Bit String. The most common method. Applicable to All databases
Uuid. string: the 128-bit UUID algorithm is also used. Encodes the generated characters into 16 bits. Applicable to databases other than PostgreSQL.
Native: select a primary key from identity, sequence, or hilo based on the specific connected database. The applicable database is determined based on the selected generation method.
Assigned: assign a value to the application for the primary key. Note that the value assignment must be completed before the save () method is called. The applicable database is determined based on the selected generation method.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.