JPA Table Generator @ tablegenerator.

Source: Internet
Author: User
Tags generator

Saving the value of the current primary key to a table in a database, and the primary key's value is obtained each time from the specified table, the way to generate the primary key is also very common. This method of generating a primary key can be applied to any database, without worrying about the incompatibility caused by the different databases.

Create a table "Tb_generator" with the following SQL script and insert two data, as shown in the SQL script.

CREATE TABLE Tb_generator (

ID int (unsigned) is not NULL auto_increment,

Gen_name varchar (255) is not NULL,

Gen_value Int (a) is 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);

After executing the SQL statement, the data in the table is shown in Figure 5.1.

Figure 5.1 Auto-generate primary key table Tb_generator

Now there are two other tables, CUSTOMER and contact, each time you create a new record, the value of the primary key is added 1, corresponding to "CUSTOMER_PK", and the value of "CONTACT_PK" is added 1.

Here is a concrete look at how to configure the primary key generation strategy to configure the "customer" table as an example, the steps are as follows.

(1) In the location of the entity tag primary key, specify the primary key generation policy as "generationtype.table", which is set 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 generated primary key policy, for example, named "Customer_gen".

@Id

@GeneratedValue (strategy = generationtype.table,generator= "Customer_gen")

Public Integer getId () {

return this.id;

}

(3) Use the @ tablegenerator tag to define the specific settings for the table generation policy, as shown in the code 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;

}

Thus, when a new entity is created with the following code, the value of "CUSTOMER_PK" in table Tb_generator will automatically be added 1, as shown in Figure 5.2.

Customereo customer = new Customereo ();

Customer.setname ("Janet");

Customer.setshortname ("Jane");

Entitymanager.persist (customer);

Figure 5.2 Table Tb_generator after adding new data

(4) @TableGenerator tag is used to set the primary key to use the data table to generate a primary key policy, which is defined 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 {};

}

There are a few issues to be aware of when using this @ tablegenerator tag.

The token can be in front of the class name, method name, and property name. And once marked in the entity, it can be used not only in this entity, but also in other entities. It is scoped to the entity class of the entire persist unit configuration.

For example, the above definition can also be written as:

@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 it is possible to label it in Contacteo. However, it is recommended to be labeled in the entity you are acting on, which helps to make it easier to view.

The L Name property represents the name of the primary key generation policy for the table, 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".

L INITIALVALUE indicates primary key value, default is 0.

L allocationsize means that each time the primary key value is increased, for example, set to 1, it will automatically add 1 each time a new record is created, and the default is 50.

L UniqueConstraint similar to the usage in the @table tag, please refer to section 5.2.1.

Use a simple schematic representation of the persisted primary key table and the table generation strategy, as shown in the figure.


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.