How to add a hibernate primary key

Source: Internet
Author: User

1. assigned:

Primary Key from externalProgramGenerates data without hibernate. ---- If the programCodeTo specify the primary key.

2. increment:

Generates auto-increment primary keys for long, short, or Int data columns. The increment primary key generation method is independent of the underlying database. Most databases, such as MySQL, MSSQL, and Oracle, support the increament generation method. 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. The disadvantage of the increment method is that when multiple threads concurrently perform write operations on database tables, the same primary key value may occur, and duplicate primary key conflicts may occur. Therefore, when multiple threads perform concurrent operations, this method should not be used.

3. Identity:

If the data column type is long, short, or Int, you can use the primary key generator to generate an Automatically increasing hibernate primary key. It is related to the underlying database and requires that the database support identify. For example, in MySQL, It is auto_increment and in SQL Server, It is identify. supported databases include MySQL, SQL Server, DB2, Sybase, and hypersonicsql. (It seems that Oracle is not supported) It does not require Hibernate and user interference and is easy to use, but it is not convenient to transplant programs between different databases. The advantage of identity is that there is no increment method and an error is sent. Set automatic growth for tables involved in the database.

4. sequence:

Use sequence in Oracle and other databases to generate primary keys. Sequence is characterized by database relevance. seqhio requires the underlying support for sequence, such as oracle.

5. HiLo:

Use hi/LoAlgorithmTo generate a primary key, you need a table to save additional primary key information. The identifier generated in this way is unique only in a specific database. Do not use this generator when using JTA (Java distributed transaction) to obtain links or links provided by users. Table information must be maintained in the Hilo mode. Therefore, the impact on data may have a certain impact.

6. seqhio:

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. This primary key generator can be used if the data column type is long, short, or Int.

7. Native:

Hibernate uses identity, Hilo, and sequence as the primary key generation method based on different database dialects. The advantage of native is that it has nothing to do with the underlying nature, this facilitates migration between different databases. hibernate selects the primary key generation method based on different databases. In Oracle, you need to create a sequence named hibernate_sequence. If the hibernate. hbm2ddl. Auto attribute is set, you do not need to create a sequence manually, provided that the database account must have the advanced permissions such as create sequence. Databases such as MySQL do not need to create sequence.

8. UUID. HEX:

The unique value is generated based on the 128-bit algorithm and compiled into a 32-bit length unique string as the primary key value, UUID. the advantage of hex is that it supports most databases. The disadvantage is that it occupies a large storage space. For systems with high requirements for concurrent insert, UUID. Hex is recommended as the primary key generation mechanism.

9. UUID. String:

Using the uuid algorithm, UUID is encoded as a string consisting of 16 characters in length and any ASCII characters. It cannot be used in a PostgreSQL database. UUID. String is similar to UUID. Hex and requires a large storage space.

10. Foreign:

Use External table fields as the hibernate primary key.

Generally, using UUID. HEX to generate a hibernate primary key will provide 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.

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.