Primary Key Generation Policy of JPA

Source: Internet
Author: User

@ Generatedvalue: The primary key generation policy, which is specified through the Strategy attribute.

The primary key generation policy is specified by generationtype. Generationtype is an enumeration that defines the type of primary key generation policy.

1. Auto automatically selects a primary key generation policy most suitable for the underlying database. For example, MySQL automatically corresponds to auto increment. This is the default option. If you only write @ generatedvalue, it is equivalent to @ generatedvalue (Strategy = generationtype. Auto ).

2. The self-increment field in the identity table. Oracle does not support this method.

3. Sequence generates primary keys through sequences. MySQL does not support this method.

4. Tables generate primary keys through tables. The framework uses the table simulation sequence to generate primary keys. Using this policy makes the application easier to port databases. Different JPA implementers generate different table names. For example, openjpa generates openjpa_sequence_table table, Hibernate generates a hibernate_sequences table, and toplink generates a sequence table. These tables have two fields, seq_name and seq_count.

In our applications, @ generatedvalue (Strategy = generationtype. Auto) is generally used to automatically select a primary key generation policy to adapt to different database migrations.

If you use hibernate to implement JPA, you can use hibernate to extend the primary key generation policy through hibernate's @ genericgenerator.

@ Genericgenerator (name = "system-UUID", strategy = "UUID") declares a general policy generator. The name is "system-UUID", and the policy strategy is "UUID ".

@ Generatedvalue (generator = "system-UUID") use the generator attribute to specify the Policy Generator to be used.

This is a method I use in projects. It generates a 32-bit string and is a unique value. The most common, applicable to all databases.

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.