@GeneratedValue four standard usages for Table,sequence,identity,auto

Source: Internet
Author: User
Tags generator

First, the JPA generic policy generator
JPA provides four standard usage by @generatedvalue source code:

    @Target ({Method,field})    
    @Retention (RUNTIME) public    
    @interface generatedvalue{    
        generationtype Strategy () default AUTO;    
        String generator () Default "";    
    }   

Among them GenerationType:

public enum generationtype{    
    TABLE,    
    SEQUENCE,    
    IDENTITY,    
    AUTO   

The four standard usages provided by JPA are table,sequence,identity,auto.
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.
IDENTITY: Primary key is automatically generated by the database (mainly auto-growth type)
AUTO: The primary key is controlled by the program.

One: TABLE

@Id  
@GeneratedValue (strategy = generationtype.table, generator= "Payablemoney_gen")  
@TableGenerator (name = " Pk_gen ",  
    table=" Tb_generator ",  
    pkcolumnname=" Gen_name ",  
    valuecolumnname=" Gen_value ",  
    Pkcolumnvalue= "PAYABLEMOENY_PK",  
    allocationsize=1  
)

This applies the table tb_generator, defined as

CREATE TABLE  tb_generator (  
  ID number not NULL,  
  gen_name VARCHAR2 (255) isn't null,  
  gen_value number not N ULL,  
  PRIMARY KEY (ID)  

Insert record to use for generating primary key

After the primary key is generated, the value of this record is incremented by Allocationsize.

Definition of @TableGenerator:

 @Target ({TYPE, METHOD, FIELD}) @Retention (RUNTIME) public @interface Tablegenerator {  
  String name ();  
  String table () default "";  
  String catalog () default "";  
  String schema () default "";  
  String pkcolumnname () default "";  
  String valuecolumnname () default "";  
  String pkcolumnvalue () default "";  
  int InitialValue () default 0;  
  int allocationsize () default 50;  
Uniqueconstraint[] Uniqueconstraints () default {}; }  

Where Property Description:
The Name property represents the names of the table primary key generation policy, which is referenced in the "generator" value set in @generatedvalue.
The Table property represents the name of the tables that are persisted by the table generation policy, for example, where the table uses "Tb_generator" in the database.
The Catalog property and schema Specify the directory name or database name where the table is located.
The value of the Pkcolumnname property represents the name of the key value in the persisted table that the primary key generates for the policy. For example, "Gen_name" as the key value of the primary key in "Tb_generator"
The value of the Valuecolumnname property represents the value that is currently generated by the primary key in the persisted table, and its value will accumulate with each creation. For example, "Gen_value" as the value of the primary key in "Tb_generator"
The value of the Pkcolumnvalue property represents the primary key corresponding to the build policy in the persisted table. For example, in the "tb_generator" table, the value of "Gen_name" is "CUSTOMER_PK".
The initialvalue represents the primary key value, which defaults to 0.
Allocationsize indicates that each time the primary key value increases in size, such as set to 1, it automatically adds 1 after each new record is created, and defaults to 50.
UniqueConstraint is similar to the usage in the @table tag.

Two: SEQUENCE

@Id  
@GeneratedValue (strategy = generationtype.sequence,generator= "Payablemoney_seq")  
@SequenceGenerator ( Name= "Payablemoney_seq", Sequencename= "Seq_payment")  

@SequenceGenerator definition

@Target ({TYPE, METHOD, FIELD})   
@Retention (RUNTIME) public  
@interface sequencegenerator {  
 String name ();  
 String sequencename () Default "";  
 int InitialValue () default 0;  
 int allocationsize () default 50;  

The Name property represents the names of the table primary key generation policy, which is referenced in the "generator" value set in @generatedvalue.
The Sequencename property represents the name of the database sequence used to generate the policy.
The initialvalue represents the primary key value, which defaults to 0.
Allocationsize indicates that each time the primary key value increases in size, such as set to 1, it automatically adds 1 after each new record is created, and defaults to 50.

Three: IDENTITY

@Id  
@GeneratedValue (strategy = generationtype.identity)  

Four: AUTO

@Id  
@GeneratedValue (strategy = Generationtype.auto)  

When you specify a primary key, if you do not specify a primary key generation policy, auto is the default.

@Id  

It's the same as the definition below.

@Id  
@GeneratedValue (strategy = Generationtype.auto)  

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.