Hibernate Primary Key Generation Method

Source: Internet
Author: User

If you set the table's primary key field id to an auto-increment numeric when using SQLServer database, set it in hibernate as follows:
 
<Id name = "id" type = "java. lang. String">
<Column name = "id" length = "50"/>
<Generator class = "native"/>
</Id>
 
Hibernate Primary Key Generation Method
 
Key Generator primary Key Generator
Option description:
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 save the Historical Status of the primary key generation.
3) seqhilo
Similar to hilo, the primary key generation mechanism implemented through the hi/lo algorithm only stores the Historical Status of the primary key in Sequence, which is suitable for databases that support Sequence, such as Oracle.
4) 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 each time the primary key needs to be generated
Add 1 as the primary key. This method may cause a problem: if multiple instances access the same database, because each instance maintains the 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 to access, this method must be avoided.
 
5) identity
Use the primary key generation mechanism provided by the database. Such as the primary key generation mechanism in DB2, SQL Server, and MySQL.
 
6) sequence
Use the sequence mechanism provided by the database to generate the primary key. For example, Sequence in Oralce.
 
7) native
Hibernate uses identity, hilo, and sequence as the primary key generation method based on the underlying database.
 
8) 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.
 
9) uuid. string
Similar to uuid. hex, the generated primary key is not encoded (Length: 16 ). Problems may occur in some databases (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 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 will be read each time data is inserted, and the incremental value will be 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, for systems with high requirements for concurrent Insert, uuid. hex is recommended as the primary key generation mechanism.

Author: ERDP Technical Architecture"

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.