JPA Table Generator generationtype. Table.

Source: Internet
Author: User

Save the current primary key value to a database table separately. The primary key value is obtained from the specified table each time. This method of generating the primary key is also very common. This method can be used to generate a primary key for any database without worrying about incompatibility between different databases.

Use the followingSQLScript to create a table"Tb_generatorAnd insert two data entries,SQLThe script is as follows.

Create Table tb_generator (

Id int (20) unsigned not null auto_increment,

Gen_name varchar (255) not null,

Gen_value int (20) not null,

Primary Key (ID)

)

 

Insert into tb_generator (gen_name, gen_value) values ('customer _ pK', 1 );

Insert into tb_generator (gen_name, gen_value) values ('Contact _ pK', 100 );

 

There are two other tables.CustomerAndContactThey generate primary key values for each new record respectivelyCustomer_pk"CorrespondingValueValue plus1,"Contact_pk"CorrespondingValueValue plus1.

The following describes how to configure the primary key generation policy to configure the"CustomerTake the table as an example.

(1) InEntityMark the location of the primary key and specify the primary key generation policy as"Generationtype. Table", The specific settings are as follows.

@ Entity

@ Table (name = "customer ")

Public final class customereo implements java. Io. serializable {

 

Private integer ID;

 

@ ID

@ Generatedvalue (Strategy = generationtype. Table)

Public integer GETID (){

Return this. ID;

}

 

Public void setid (integer ID ){

This. ID = ID;

}

}

(2) Specifies the name of the primary key generation policy. For example,Customer_gen".

@ ID

@ Generatedvalue (Strategy = generationtype. Table,Generator = "customer_gen")

Public integer GETID (){

Return this. ID;

}

(3) Use@ TablegeneratorTag The specific settings of the Table Generation Policy,CodeAs shown below.

@ ID

@ Generatedvalue (Strategy = generationtype. Table, generator = "customer_gen ")

@ Tablegenerator (name = "customer_gen ",

Table = "tb_generator ",

Pkcolumnname = "gen_name ",

Valuecolumnname = "gen_value ",

Pkcolumnvalue = "customer_pk ",

Allocationsize = 1

)

Public integer GETID (){

Return this. ID;

}

In this way, after the following code creates a new object, the tableTb_generatorMedium"Customer_pk"ValueWill automatically add1,5.2.

Customereo customer = new customereo ();

Customer. setname ("Janet ");

Customer. setshortname ("Jane ");

Entitymanager. persist (customer );

(4)@ TablegeneratorFlag is used to set a primary key to generate a primary key using a data table. Its definition is as follows.

@ 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 {};

}

When using this@ TablegeneratorWhen marking, pay attention to the following issues.

LThis tag can be placed before the class name, method name, and attribute name. Once marked in an object, it can be used not only in this object, but also in other entities. It applies to the wholePersist UnitIn the configured OBJECT class.

For example, the preceding definition can also be written as follows:

@ Entity

@ Table (name = "customer ")

@ Tablegenerator (name = "customer_gen ",

Table = "tb_generator ",

Pkcolumnname = "gen_name ",

Valuecolumnname = "gen_value ",

Pkcolumnvalue = "customer_pk ",

Allocationsize = 1

)

Public class customereo implements java. Io. serializable {

......

}

Or mark it inContacteo. However, it is recommended that you label the object to be applied to facilitate viewing.

LNameAttribute indicates the name of the table's primary key generation policy, which is referenced in@ Generatedvalue"Generator"Value.

LTableAttribute indicates the name of the table that is persisted by the table generation policy. For example, the table uses the"Tb_generator".

LCatalogAttributes andSchemaSpecify the directory name or Database Name of the table.

LPkcolumnnameThe attribute value indicates the name of the key value corresponding to the primary key generation policy in the persistence table. For example,Tb_generator"Lieutenant General"Gen_name"As the primary key value

LValuecolumnnameThe attribute value indicates the value currently generated by the primary key in the persistence table. The value of this attribute is accumulated with each creation. For example,Tb_generator"Lieutenant General"Gen_value"As the primary key value

LPkcolumnvalueThe attribute value indicates the primary key corresponding to the generation policy in the persistence table. For example,Tb_generatorIn theGen_name"Value is"Customer_pk".

LInitialvalueIndicates the first recognized primary key value. The default value is0.

LAllocationsizeIndicates the size of each increase in the primary key value, for example, set1Is automatically added after each new record is created.1, Default value:50.

LUniqueconstraintAnd@ TableThe usage in the tag is similar,

 

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.