JPA table generator @TableGenerator

Table Generator @ TableGenerator

September 30, 2013   ⁄ general  ⁄ total 3581 words ⁄  small  , medium and  large  ⁄  Comments are closed
 

 The value of the current primary key is stored separately in a database table, and the value of the primary key is obtained by querying the specified table each time. This method of generating the primary key is also very common. This method of generating primary keys can be applied to any database without worrying about the incompatibility of different databases.

Use the following SQL script to create a table "tb_generator" and insert two pieces of data, the SQL 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);

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

Figure 5.1 Automatically generate primary key table tb_generator

Now there are two other tables, customer and contact. The values ​​of the primary keys that they generate each time a new record is created add 1 to the value corresponding to "CUSTOMER_PK", and add 1 to the value corresponding to "CONTACT_PK".

Let's take a look at how to configure the generation strategy of the primary key in detail. Taking the configuration of the "customer" table as an example, the steps are as follows.

(1) In the location where the Entity marks the primary key, specify the primary key generation strategy as "GenerationType.TABLE", and 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) Specify the name of the generated primary key strategy, such as "customer_gen" here.

         @Id

         @GeneratedValue(strategy = GenerationType.TABLE,generator="customer_gen")

         public Integer getId() {

                   return this.id;

         }

(3) Use the @TableGenerator tag to define the specific settings of the table generation strategy, the code is as follows.

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

         }

这样,当通过以下代码创建新的实体后,表tb_generator中“CUSTOMER_PK”的value的值将自动加1,如图5.2所示。

             CustomerEO customer = new CustomerEO();

             customer.setName("Janet");

             customer.setShortName("Jane");

             entityManager.persist(customer);

图5.2  添加新数据后表tb_generator

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

}

在使用此@ TableGenerator标记时,需要注意以下几个问题。

l         该标记可以在类名、方法名、和属性名前。并且一旦在实体中标记,它不仅可以在本实体中使用,在其他的实体中也可以引用。它的作用范围是整个persist unit配置的实体类中。

例如以上的定义也可以写成:

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

         ……

}

或者将其标注在ContactEO中,也是可以的。但建议标注在所作用的实体中,这样有助于方便查看。

l         name属性表示该表主键生成策略的名称,它被引用在@GeneratedValue中设置的“generator”值中。

l         table属性表示表生成策略所持久化的表名,例如,这里表使用的是数据库中的“tb_generator”。

l         catalog属性和schema具体指定表所在的目录名或是数据库名。

l         pkColumnName属性的值表示在持久化表中,该主键生成策略所对应键值的名称。例如在“tb_generator”中将“gen_name”作为主键的键值

l         valueColumnName属性的值表示在持久化表中,该主键当前所生成的值,它的值将会随着每次创建累加。例如,在“tb_generator”中将“gen_value”作为主键的值

l         pkColumnValue属性的值表示在持久化表中,该生成策略所对应的主键。例如在“tb_generator”表中,将“gen_name”的值为“CUSTOMER_PK”。

l         initialValue表示主键初识值,默认为0。

l         allocationSize表示每次主键值增加的大小,例如设置成1,则表示每次创建新记录后自动加1,默认为50。



 来源:http://blog.csdn.net/EJB_JPA/archive/2008/05/09/2422370.aspx

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326615179&siteId=291194637