Hibernate primary key Generation policy

Source: Internet
Author: User

The main key generation strategies in Hibernate are as follows:

(1) UUID

The principle is to use the 128-bit UUID algorithm to generate the primary key, which ensures consistency in the network environment. When you use this build policy, the primary key of the entity class is of type string and is mapped into a table with a varchar field. Applies to all databases.

[Java]View Plaincopyprint?
    1. <id name="id" column="id" >
    2. <span style="White-space:pre" > </span><generator class="UUID" ></generator>
    3. </id>

(2) native

The principle is to automatically select sequence, identify, and Hilo based on the database type. When using this build strategy, the entity class's primary key is of type int, which is mapped into a table field int, and if MySQL, the ID is self-growing.

[Java]View Plaincopyprint?
    1. <id name="id" column="id" >
    2. <generator class="native" ></generator>
    3. </id>

(3) Identity

The principle is for MySQL, DB2, MS SQL Server, which uses a database-generated primary key to generate a unique identity for long, short, and int types.

(4) Increment

Used to generate a unique identity for a long, short, or int type. It can only be used if no other process is inserting data into the same table. Do not use under the cluster.

(5) Sequence

In Db2,postgresql, Oracle, SAP DB, Mckoi use sequences (sequence), and generators (generator) are used in InterBase. The returned identifiers are of type long, short, or int.

(6) Hilo

Use a high/low algorithm to efficiently generate identifiers for long, short, or int types. Given a table and a field (the default is Hibernate_unique_key and Next_hi, respectively) as the source of the high value. The high/low algorithm generates identifiers that are unique only in a particular database.

When using annotations, the default is: @GeneratedValue, equivalent to the native policy.

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.