Hibernate primary Key generation policy

Source: Internet
Author: User
Tags uuid uuid identifier

Represents the Fuzhou Builder

Describe

Increment

Automatically generates the table identifier incrementally in increments of 1 per increment by hibernate

Identity

The table identifier is generated by the underlying database. The condition is that the database supports auto-grow data types.

Sequence

Hibernate generates identifiers based on the underlying database sequence. The condition is that the database supports sequences.

Native

Select identity, Sequence, Hilo based on the ability of the underlying database to automatically generate the representation

Uuid.hex

Hibernate uses a 128-bit UUID algorithm to generate identifiers. This algorithm can generate a unique string identifier in the network environment, which is not popular because the primary key of the string type takes up more database space than the primary key of the integer type.

If the primary key is in character type and does not represent any meaning.

Assigned

Applies to natural primary keys. The Java program is responsible for generating identifiers. The SetID () method cannot be declared private. Try to avoid using natural primary keys.

Increment Identifier Generator
The increment identifier generator is incremented by Hibernate to assign a value to the surrogate primary key
Hibernate reads the maximum value of the primary key in the news table first, and the next time you insert a record into the news table, it increments on the basis of Max (ID), which is incremented by 1. (Take away + 1)
Scope of application:
Because the increment survival identifier mechanism is not dependent on the underlying database system, it is suitable for all database systems
For applications where only a single Hibernate application process accesses the same database
The OID must be a long, int, or short type, and if the OID is defined as a byte type, an exception will be thrown at run time
Identity Identifier Generator
The identity identifier generator is responsible for generating identifiers by the underlying database, which requires the underlying database to define the primary key as the autogrow field type (plus 1 takes away)
Scope of application:
Because the identity generation identifier mechanism relies on the underlying database system, it is required that the underlying database system must support the Autogrow field type. Databases that support autogrow field types include: DB2, Mysql, MSSQLServer, Sybase, etc.
The OID must be a long, int, or short type, and if the OID is defined as a byte type, an exception will be thrown at run time
Sequence Identifier Generator
The sequence identifier generator uses the sequence provided by the underlying database to generate identifiers.
Hibernate when persisting a News object, first obtains a unique identification number from the NEWS_SEQ sequence of the underlying database, and then takes it as the primary key value
Scope of application:
Because the mechanism of sequence generates identifiers relies on the sequence of underlying database systems, it is required that the underlying database system must support sequences. Databases that support sequences include: DB2 Oracle, etc.
The OID must be a long, int, or short type, and if the OID is defined as a byte type, an exception will be thrown at run time
Native Identifier Generator
The native identifier generator chooses to use the identity, sequence, or Hilo identifier generator, depending on the underlying database's ability to automatically generate identifiers.
Scope of application:
Because native can automatically select the appropriate identifier generator based on the type of underlying database system, it is well suited for cross-database platform development
The OID must be a long, int, or short type, and if the OID is defined as a byte type, an exception will be thrown at run time
assigned identifier generator
Hibernate and the underlying database do not help you generate a primary key, which means you have to manually set the value of the primary key in your program.
Scope of application:
The primary key has a certain meaning and needs to be generated according to the business situation.
UUID Identifier Generator
Hibernate uses a 128-bit UUID algorithm to generate identifiers. This algorithm can generate a unique string identifier in a network environment, which is not popular because the primary key of the string type takes up more database space than the primary key of the integer type
scope of Use:
The primary key is a string and must be a unique

Hibernate primary Key generation policy

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.