A small summary of Hibernate's primary key generation strategy

Source: Internet
Author: User

The primary key generation strategy is broadly divided into two types:

  • Manual control Strategy
  • Auto-Generate policy "framework auto-generation and database auto-generation"

Manual Control Strategy:

Assigned: The type is arbitrary and requires the encoding staff to manually set the primary key value before the Save () to the database, that is, to invoke the object's setter method for the assignment operation.

Note: It is possible to cross a database, but manual control is not guaranteed to be non-repeatable and not recommended.

Automatically generate policies:

1. UUID: Automatically generate 32-bit and above random string, generated by including but not limited to network card address, time value and so on.

Note: Can be cross-database, high efficiency, can guarantee uniqueness, recommended to use "Although Occupy space large"

2. Increment: Automatically gets the maximum value of the primary key in the database "integer", and automatically adds a value to the object.

Note: You can cross a database, but not for use under a cluster, and the multi-threaded concurrent update database will fetch the same primary key value.

3. Identity: The database primary key is set to auto-Grow "integer".

Note: For Mysql,db2,sqlserver, not for Oracle.

4. Sequence: The database primary key is set to auto-Grow "integer".

Note: Applies to Oracle.

5. One of the most common forms of generation in hilo:hibernate requires an extra table to hold the HI value. The table that holds the HI value has at least one record (only related to the first record), or an error occurs.

<IDname= "id"column= "id"><Generatorclass= "Hilo"><paramname= "Table">Hibernate_hilo</param>        <paramname= "column">Next_hi</param><paramname= "Max_lo">100</param></Generator></ID><!--Specifies the name of the table that holds the HI value -<paramname= "Table">Hibernate_hilo</param><!--Specify column names to hold hi values -<paramname= "column">Next_hi</param> <!--Specify the maximum value of the low -<paramname= "Max_lo">100</param> <!--You can also omit the table and column configurations, whose default table is Hibernate_unique_key, which is listed as Next_hi -<IDname= "id"column= "id"><Generatorclass= "Hilo"><paramname= "Max_lo">100</param></Generator></ID>

The Hilo generator process for generating a primary key (in the Hibernate_unique_key table, next_hi column example):

    • Get Hi Value: Reads and records the value of the Next_hi field in the database's Hibernate_unique_key table, and the value of this field in the database is saved by 1.
    • Get the LO value: from 0 to Max_lo Loop, the difference is 1, the value is Max_lo, and the LO value continues to loop from 0 to Max_lo.
    • Generates a primary key value based on the formula Hi * (Max_lo + 1) + lo calculation.

Note: When the Hi value is 0, then the first value is not 0* (max_lo+1) +0=0, but Lo skips 0 starting from 1, directly 1, 2, 3 ...

How much is the Max_lo configuration appropriate?

This depends on the situation, if the system generally does not restart, and the need to use this table to establish a large number of primary keys, you can max_lo configuration larger, so you can reduce the number of reading data tables, improve efficiency; Conversely, if the server is often restarted, you can max_lo a smaller configuration, You can avoid a large interval between each reboot of the primary key, which causes the primary key value of the primary key to be incoherent.

Note: You can cross-database, the flags generated by the Hilo algorithm can only remain unique in one database.

6. Native:native by hibernate according to the database used by the use of identity, Hilo, sequence one of the primary key generation mode, flexibility is strong. If the identity is supported, the identity is used and sequence is used if sequence is supported.

Note: According to the automatic selection of the database, if you use multiple databases in the project, you can use this method, you need to set the table's self-increment field or set up a sequence, create a table, etc. 7. Foreign: Used in a one-to-one association relationship.

<IDname= "id"column= "id"><Generatorclass= "foreign"><paramname= "Property">User</param></Generator></ID><one-to-onename= "User"class= "Com.msym.domain.User"constrained= "true" />
Note: Less use, only one-to-one association relationship "a single relationship is usually a table."

Small summary:

Primary key generation Policy

Who generated the OID

Database ID field Type

Compatible databases

Assigned

Programmer control Input

Not Limited

All

Uuid

Hibernate internal Control input

String type

All

Increment

Hibernate internal Control input

Integral type

All

Identity

Database underlying control input

Integer self-increment

MySQL Available

Sequence

Database underlying control input

Integral type

Oracle Available

Native

Database underlying control input

Integer self-increment

All

A small summary of Hibernate's primary key generation strategy

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.