Hibernate annotation configuration of the primary key generation policy

Source: Internet
Author: User
Tags generator uuid

Hibernate supports a total of 13 build policies by default:


1. Increment

2. Identity

3. Sequence


4. Hilo

5. Seqhilo

6. UUID


7. Uuid.hex

8. Guid

9. Native


Ten. Assigned

One. Select

Foreign.

Sequence-identity.

Here are a few of the more common strategies:


①identity [Natural Increment]

Supports Db2,mysql,sql server,sybase and Hypersonicsql databases for generating unique identities for long or short or int types. It relies on different databases at the bottom,
Not related to Hibernate and programmers.

Examples of annotations:

@Id

@GenericGenerator (name = "Idgenerator", strategy = "identity")

@GeneratedValue (generator = "Idgenerator")

②sequence [sequence]

Databases such as ORACLE,DB2,POSTGRESQL,SAPDB are supported for generating unique identities for long or short or int types. It requires support from the underlying database,
The sequence sequence is maintained by the database.

Examples of annotations:

@Id

@GenericGenerator (name = "Idgenerator", strategy = "sequence",

Parameters = {@Parameter (name = "Sequence", value= "Seq_name")})

@GeneratedValue (generator = "Idgenerator")

Note: This policy requires that the sequence name be set, otherwise hibernate will not be found, which will cause an exception to be thrown:

Org.hibernate.exception.SQLGrammarException:could not get Next sequence value

③native

Support for the underlying database is required, and for Oracle, the sequence policy is used for Mysql,sql Server's identity generation strategy.

Examples of annotations:

@Id

@GenericGenerator (name = "Idgenerator", strategy = "native")

@GeneratedValue (generator = "Idgenerator")

④increment [Natural Increment]

Unlike the identity policy, the policy does not rely on the underlying database, but relies on hibernate itself to generate a unique identity for a long or short or int type.
The primary key counter is maintained by one instance of hibernate, each with a self-increment of 1, but cannot be used under the cluster.
Otherwise, a primary key violation is caused, and the policy applies to all relational database uses.

Examples of annotations:

@Id

@GenericGenerator (name = "Idgenerator", strategy = "increment")

@GeneratedValue (generator = "Idgenerator")

⑤uuid [string of 32-bit 16 binary numbers]

Using the 128-bit UUID algorithm to generate the primary key can guarantee the uniqueness of the primary key under the network environment, and it can guarantee the uniqueness of the primary key under different database and different server.
The UUID is eventually encoded into a 32-bit, 16-digit string,
Occupy a large amount of storage space. Used to generate a unique identity for the String type, applicable to all relational databases.

Examples of annotations:

@Id

@GenericGenerator (name = "Idgenerator", strategy = "uuid")

@GeneratedValue (generator = "Idgenerator")

⑤assigned [manually assigning primary key ID values]

This policy requires the programmer to maintain and manage the primary key on its own, and when the data needs to be stored, the programmer must specify a primary key ID value for the data assignment.
If the data is not assigned the primary key ID value or the assigned value is duplicated, the data will not be persisted and will cause an exception to be thrown.

Examples of annotations:

@Id

@GenericGenerator (name = "Idgenerator", strategy = "assigned")

@GeneratedValue (generator = "Idgenerator")

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.