Hibernate duplicate entry Solution

Source: Internet
Author: User
Tags db2

Today, when I was modifying a bug, I couldn't find the cause. I checked the code multiple times and found that the logic was correct, but the operation would indeed cause intermittent errors, after a long time, when I found an error, the background reported a "duplicate entry '000000' for key 'primary'" error, which literally means that the primary key is repeated, this means that there are two operations on this table. An Automatic Program outside the framework will add data to this table, and you can also manually add data in the framework, the cause of this error is that the records added by the Program outside the framework conflict with the manually added Record ID.

To understand the cause of this problem, you must understand the meaning of the different attribute values of hibernate <generator class = "native"> </generator> class.

1) assigned
The primary key is generated by an external program and does not involve hibernate.

2) HiLo
Use hi/Lo
The primary key generation mechanism implemented by the algorithm requires additional database tables to save the Historical Status of the primary key generation.

3) seqhilo
And HiLo
Similarly, 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 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 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
Database Sequence
Mechanism to generate a 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
Hibernate is based on 128
Bit unique value generation algorithm to generate a hexadecimal value (encoded with a length of 32
As the primary key.

9) UUID. String
And UUID. HEX
Similarly, 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, sqlserver, and MySQL
And provides an easy-to-use primary key generation mechanism (auto-increase ).
Field 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 produces
. Therefore, UUID. Hex is recommended for systems with high requirements for concurrent insert.
Generate as primary key
Mechanism.

This bug occurs because multiple instances access the same data table. In the framework, Hibernate maintains a variable in the current application instance to store the maximum value of the current ID, this value will be added to 1 as the primary key every time a primary key is generated. At this time, data has been added to the program outside the framework. The current primary key is used for record addition by the Program outside the framework, therefore, an error occurs. To solve this problem, set <generator class ="
Change the increment of increment "> </generator> to native.

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.