JPA annotation @generatedvalue and its strategy enumeration

Source: Internet
Author: User
Tags generator
JPA annotation @generatedvalue and its strategy enumeration

1. Source Code

@Target ({elementtype.method, Elementtype.field})
@Retention (retentionpolicy.runtime) public
@interface Generatedvalue {
    generationtype strategy () default Generationtype.auto;

    String generator () Default "";
}
public enum GenerationType {
    TABLE,
    SEQUENCE,
    IDENTITY,
    AUTO;

    Private GenerationType () {
    }
}

2. Introduce

-table: Use a specific database table to save the primary key.
 -SEQUENCE: Generates a primary key based on the sequence of the underlying database, provided the database supports a sequence.
 -IDENTITY: Primary keys are automatically generated by the database (mainly automatic growth)
 -Auto: Primary keys are controlled by the program. 

If the database primary key uses the MySQL automatic generation policy, the following definition is required to properly save
Otherwise throw cannot find generator exception

@Id
@GeneratedValue (strategy = generationtype.identity)
private Long pkid;

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.