Primary Key Generation Policy of hibernate annotation

Source: Internet
Author: User

Hibernate supports a total of 13 generation policies by default:


1. increment 2. identity 3. sequence


4. hilo 5. seqhilo 6. uuid


7. uuid. hex 8. guid 9. native


10. assigned 11. select 12. foreign 13. sequence-identity

 

The following describes several common strategies:


① Identity [incremental]

Supports DB2, MySQL, SQL Server, Sybase, and HypersonicSQL databases to generate unique identifiers for long, short, or int types. It depends on different underlying databases,
It has nothing to do with Hibernate and programmers.

Annotation example:

@ Id

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

@ GeneratedValue (generator = "idGenerator ")

 

② Sequence [sequence]

Supports Oracle, DB2, PostgreSql, SAPDb, and other databases to generate unique identifiers for long, short, or int types. It requires support from the underlying database,
The database maintains the sequence.

Annotation example:

@ Id

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

Parameters = {@ Parameter (name = "sequence", value = "seq_name ")})

@ GeneratedValue (generator = "idGenerator ")

Note: this policy requires the sequence name to be set; otherwise, hibernate will not be able to find the sequence name, which will cause an exception:

Org. hibernate. exception. SQLGrammarException: cocould not get next sequence value

 

③ Native

The underlying database is supported. For MySQL, SQL Server adopts the identity generation policy and sequence policy for Oracle.

Annotation example:

@ Id

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

@ GeneratedValue (generator = "idGenerator ")

 

④ Increment [incremental]

Unlike the identity Policy, this policy does not depend on the underlying database, but on hibernate itself, which is used to generate a unique identifier for long, short, or int type.
The primary key counter is maintained by an instance of hibernate. Each auto increment is 1, but this policy cannot be used in the cluster,
Otherwise, a primary key conflict occurs. This policy applies to all relational databases.

Annotation example:

@ Id

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

@ GeneratedValue (generator = "idGenerator ")

 

⑤ Uuid [32-bit hexadecimal string]

The 128-bit UUID algorithm is used to generate a primary key. This ensures the uniqueness of the primary key in the network environment and the uniqueness of the primary key in different databases and servers.
Uuid is eventually encoded into a 32-bit hexadecimal string,
The occupied storage space is large. Used to generate a unique identifier for the String type, applicable to all relational databases.

Annotation example:

@ Id

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

@ GeneratedValue (generator = "idGenerator ")

 

⑤ Assigned [manually allocate the primary key ID value]

This policy requires the programmer to maintain and manage the primary key. When data needs to be stored, the programmer must assign a primary key ID value for the data,
If the data is not assigned a primary key ID value or the assigned value already exists, the data will not be persistent and will cause exceptions.

Annotation example:

@ Id

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

@ GeneratedValue (generator = "idGenerator ")

Related Article

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.