JPA Hibernate uses UUID as the primary key problem

Source: Internet
Author: User
Tags uuid

1. Set the primary key in the database to varchar (32).

2. Write @GenericGenerator to the class header in entity (name = "Jpa-uuid", strategy = "uuid")

3. Write @GeneratedValue (generator = "Jpa-uuid") at the top of the ID primary key in entity Note the value in generator must be exactly the same as the Name property in Comment @genericgenerator.

4. Set the primary key ID in entity to string type. Set length to 32

5.GenericGenerator is a comment in hibernate and has two parameters. The name is System-uuid "" and the policy strategy is the UUID.

[Email protected] supports 13 different strategies, namely:

static {Generators. put ("UUID", Uuidhexgenerator. Class); Generators. put ("Hilo", Tablehilogenerator. Class); Generators. put ("Assigned", assigned. Class); Generators. put ("Identity", Identitygenerator. Class); Generators. put ("Select", Selectgenerator. Class); Generators. put ("Sequence", Sequencegenerator. Class); Generators. put ("Seqhilo", Sequencehilogenerator. Class); Generators. put ("Increment", Incrementgenerator. Class); Generators. put ( "foreign", Foreigngenerator.class) .put ( "GUID", Guidgenerator.class" ; Generators.put ( "Uuid.hex", Uuidhexgenerator.class) .put (" sequence-identity ", Sequenceidentitygenerator.class)  The above 12 strategies, plus native, are altogether 13 kinds. The 
[email protected] annotations belong to a JPA interface (starting with Java EE 5, exists under the Javax.persistence package), its interface contains two abstract parameters, GenerationType type strategy and string type generator, and two parameters have corresponding default values. &NBSP

8.GenerationType is also located under the Javax.persistence package, and also inherits the Enum enum class, and there are 4 values for us to use, respectively:
Table: Use a specific database table to hold the primary key.
SEQUENCE: Generates a primary key based on the sequence of the underlying database, provided the database supports the sequence. This value is used with generator, generator specifies the generator used to generate the primary key (possibly a sequence written by itself in Orcale).
IDENTITY: The primary key is automatically generated by the database (primarily support for autogrow databases, such as MySQL)
AUTO: The primary key is controlled by the program and is also the default value of GenerationType.

9. Two databases support for GenerationType

Mysql
Generationtype.table
Generationtype.auto
Generationtype.identity
Generationtype.sequence not supported

Oracle
Strategy=generationtype.auto
Generationtype.sequence
Generationtype.table
Generationtype.identity not supported

JPA Hibernate uses UUID as the primary key problem

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.