---
title: "@GeneratedValue"
author: "lvjun106"
date: "2015-03-21T15:58:16Z"
tags: []
categories: ["开源软件"]
slug: "generatedvalue"
draft: false
lastmod: "2015-03-21T15:58:16Z"
id: 2194464
url: "https://lvjun106.iteye.com/blog/2194464"
---

转\[[http://blog.csdn.net/fancylovejava/article/details/7438660](http://blog.csdn.net/fancylovejava/article/details/7438660)]

 

一、JPA通用策略生成器   
通过annotation来映射hibernate实体的,基于annotation的hibernate主键标识为@Id,   
其生成规则由@GeneratedValue设定的.这里的@id和@GeneratedValue都是JPA的标准用法,   
JPA提供四种标准用法,由@GeneratedValue的源代码可以明显看出. 

Java代码  [![收藏代码](http://xiaogui9317170.iteye.com/images/icon_star.png)](http://xiaogui9317170.iteye.com/blog/283526 "收藏这段代码")

1. @Target({METHOD,FIELD})
2. @Retention(RUNTIME)
3. public @interface GeneratedValue{
4. GenerationType strategy() default AUTO;
5. String generator() default "";
6. }

其中GenerationType: 

Java代码  [![收藏代码](http://xiaogui9317170.iteye.com/images/icon_star.png)](http://xiaogui9317170.iteye.com/blog/283526 "收藏这段代码")

1. public enum GenerationType{
2. TABLE,
3. SEQUENCE,
4. IDENTITY,
5. AUTO
6. }

JPA提供的四种标准用法为TABLE,SEQUENCE,IDENTITY,AUTO.   
TABLE：使用一个特定的数据库表格来保存主键。   
SEQUENCE：根据底层数据库的序列来生成主键，条件是数据库支持序列。   
IDENTITY：主键由数据库自动生成（主要是自动增长型）   
AUTO：主键由程序控制。 

1、TABLE 

Java代码  [![收藏代码](http://xiaogui9317170.iteye.com/images/icon_star.png)](http://xiaogui9317170.iteye.com/blog/283526 "收藏这段代码")

1. @Id
2. @GeneratedValue(strategy = GenerationType.TABLE, generator="payablemoney\_gen")
3. @TableGenerator(name = "pk\_gen",
4. table="tb\_generator",
5. pkColumnName="gen\_name",
6. valueColumnName="gen\_value",
7. pkColumnValue="PAYABLEMOENY\_PK",
8. allocationSize=1
9. )

这里应用表tb\_generator，定义为 

Sql代码  [![收藏代码](http://xiaogui9317170.iteye.com/images/icon_star.png)](http://xiaogui9317170.iteye.com/blog/283526 "收藏这段代码")

1. CREATE TABLE  tb\_generator (
2. id NUMBER NOT NULL,
3. gen\_name VARCHAR2(255) NOT NULL,
4. gen\_value NUMBER NOT NULL,
5. PRIMARY KEY(id)
6. )

插入纪录，供生成主键使用， 

Sql代码  [![收藏代码](http://xiaogui9317170.iteye.com/images/icon_star.png)](http://xiaogui9317170.iteye.com/blog/283526 "收藏这段代码")

1. INSERT INTO tb\_generator(id, gen\_name, gen\_value) VALUES (1,PAYABLEMOENY\_PK', 1);

在主键生成后，这条纪录的value值，按allocationSize递增。 

@TableGenerator的定义： 

Java代码  [![收藏代码](http://xiaogui9317170.iteye.com/images/icon_star.png)](http://xiaogui9317170.iteye.com/blog/283526 "收藏这段代码")

01. @Target({TYPE, METHOD, FIELD})
02. @Retention(RUNTIME)
03. public @interface TableGenerator {
04. String name();
05. String table() default "";
06. String catalog() default "";
07. String schema() default "";
08. String pkColumnName() default "";
09. String valueColumnName() default "";
10. String pkColumnValue() default "";
11. int initialValue() default 0;
12. int allocationSize() default 50;
13. UniqueConstraint\[] uniqueConstraints() default {};
14. }

其中属性说明：   
name属性表示该表主键生成策略的名称，它被引用在@GeneratedValue中设置的“generator”值中。   
table属性表示表生成策略所持久化的表名，例如，这里表使用的是数据库中的“tb\_generator”。   
catalog属性和schema具体指定表所在的目录名或是数据库名。   
pkColumnName属性的值表示在持久化表中，该主键生成策略所对应键值的名称。例如在“tb\_generator”中将“gen\_name”作为主键的键值   
valueColumnName属性的值表示在持久化表中，该主键当前所生成的值，它的值将会随着每次创建累加。例如，在“tb\_generator”中将“gen\_value”作为主键的值   
pkColumnValue属性的值表示在持久化表中，该生成策略所对应的主键。例如在“tb\_generator”表中，将“gen\_name”的值为“CUSTOMER\_PK”。   
initialValue表示主键初识值，默认为0。   
allocationSize表示每次主键值增加的大小，例如设置成1，则表示每次创建新记录后自动加1，默认为50。   
UniqueConstraint与@Table标记中的用法类似。 

2、SEQUENCE 

Java代码  [![收藏代码](http://xiaogui9317170.iteye.com/images/icon_star.png)](http://xiaogui9317170.iteye.com/blog/283526 "收藏这段代码")

1. @Id
2. @GeneratedValue(strategy = GenerationType.SEQUENCE,generator="payablemoney\_seq")
3. @SequenceGenerator(name="payablemoney\_seq", sequenceName="seq\_payment")

@SequenceGenerator定义 

Java代码  [![收藏代码](http://xiaogui9317170.iteye.com/images/icon_star.png)](http://xiaogui9317170.iteye.com/blog/283526 "收藏这段代码")

1. @Target({TYPE, METHOD, FIELD})
2. @Retention(RUNTIME)
3. public @interface SequenceGenerator {
4. String name();
5. String sequenceName() default "";
6. int initialValue() default 0;
7. int allocationSize() default 50;
8. }

name属性表示该表主键生成策略的名称，它被引用在@GeneratedValue中设置的“generator”值中。   
sequenceName属性表示生成策略用到的数据库序列名称。   
initialValue表示主键初识值，默认为0。   
allocationSize表示每次主键值增加的大小，例如设置成1，则表示每次创建新记录后自动加1，默认为50。 

3、IDENTITY 

Java代码  [![收藏代码](http://xiaogui9317170.iteye.com/images/icon_star.png)](http://xiaogui9317170.iteye.com/blog/283526 "收藏这段代码")

1. @Id
2. @GeneratedValue(strategy = GenerationType.IDENTITY)

4、AUTO 

Java代码  [![收藏代码](http://xiaogui9317170.iteye.com/images/icon_star.png)](http://xiaogui9317170.iteye.com/blog/283526 "收藏这段代码")

1. @Id
2. @GeneratedValue(strategy = GenerationType.AUTO)

在指定主键时，如果不指定主键生成策略，默认为AUTO。 

Java代码  [![收藏代码](http://xiaogui9317170.iteye.com/images/icon_star.png)](http://xiaogui9317170.iteye.com/blog/283526 "收藏这段代码")

1. @Id

跟下面的定义是一样的。 

Java代码  [![收藏代码](http://xiaogui9317170.iteye.com/images/icon_star.png)](http://xiaogui9317170.iteye.com/blog/283526 "收藏这段代码")

1. @Id
2. @GeneratedValue(strategy = GenerationType.AUTO)

二、hibernate主键策略生成器   
hibernate提供多种主键生成策略，有点是类似于JPA，有的是hibernate特有：   
native: 对于 oracle 采用 Sequence 方式，对于MySQL 和 SQL Server 采用identity（自增主键生成机制），native就是将主键的生成工作交由数据库完成，hibernate不管（很常用）。   
uuid: 采用128位的uuid算法生成主键，uuid被编码为一个32位16进制数字的字符串。占用空间大（字符串类型）。   
hilo: 使用hilo生成策略，要在数据库中建立一张额外的表，默认表名为hibernate\_unique\_key,默认字段为integer类型，名称是next\_hi（比较少用）。   
assigned: 在插入数据的时候主键由程序处理（很常用），这是 &lt;generator&gt;元素没有指定时的默认生成策略。等同于JPA中的AUTO。   
identity: 使用SQL Server 和 MySQL 的自增字段，这个方法不能放到 Oracle 中，Oracle 不支持自增字段，要设定sequence（MySQL 和 SQL Server 中很常用）。   
          等同于JPA中的INDENTITY。   
select: 使用触发器生成主键（主要用于早期的数据库主键生成机制，少用）。   
sequence: 调用底层数据库的序列来生成主键，要设定序列名，不然hibernate无法找到。   
seqhilo: 通过hilo算法实现，但是主键历史保存在Sequence中，适用于支持 Sequence 的数据库，如 Oracle（比较少用）   
increment: 插入数据的时候hibernate会给主键添加一个自增的主键，但是一个hibernate实例就维护一个计数器，所以在多个实例运行的时候不能使用这个方法。   
foreign: 使用另外一个相关联的对象的主键。通常和&lt;one-to-one&gt;联合起来使用。   
guid: 采用数据库底层的guid算法机制，对应MYSQL的uuid()函数，SQL Server的newid()函数，ORACLE的rawtohex(sys\_guid())函数等。   
uuid.hex: 看uuid，建议用uuid替换。   
sequence-identity: sequence策略的扩展，采用立即检索策略来获取sequence值，需要JDBC3.0和JDK4以上（含1.4）版本 

hibernate提供了多种生成器供选择,基于Annotation的方式通过@GenericGenerator实现.   
hibernate每种主键生成策略提供接口org.hibernate.id.IdentifierGenerator的实现类,如果要实现自定义的主键生成策略也必须实现此接口. 

Java代码  [![收藏代码](http://xiaogui9317170.iteye.com/images/icon_star.png)](http://xiaogui9317170.iteye.com/blog/283526 "收藏这段代码")

01. public interface IdentifierGenerator {
02. /\**
03. * The configuration parameter holding the entity name
04. \*/
05. public static final String ENTITY\_NAME = "entity\_name";
06. /\**
07. * Generate a new identifier.
08. * @param session
09. * @param object the entity or toplevel collection for which the id is being generated
10. \*
11. * @return a new identifier
12. * @throws HibernateException
13. \*/
14. public Serializable generate(SessionImplementor session, Object object)
15. throws HibernateException;
16. }

IdentifierGenerator提供一generate方法,generate方法返回产生的主键. 

三、@GenericGenerator   
自定义主键生成策略，由@GenericGenerator实现。   
hibernate在JPA的基础上进行了扩展，可以用一下方式引入hibernate独有的主键生成策略，就是通过@GenericGenerator加入的。 

比如说，JPA标准用法 

Java代码  [![收藏代码](http://xiaogui9317170.iteye.com/images/icon_star.png)](http://xiaogui9317170.iteye.com/blog/283526 "收藏这段代码")

1. @Id
2. @GeneratedValue(GenerationType.AUTO)

就可以用hibernate特有以下用法来实现 

Java代码  [![收藏代码](http://xiaogui9317170.iteye.com/images/icon_star.png)](http://xiaogui9317170.iteye.com/blog/283526 "收藏这段代码")

1. @GeneratedValue(generator = "paymentableGenerator")
2. @GenericGenerator(name = "paymentableGenerator", strategy = "assigned")

@GenericGenerator的定义: 

Java代码  [![收藏代码](http://xiaogui9317170.iteye.com/images/icon_star.png)](http://xiaogui9317170.iteye.com/blog/283526 "收藏这段代码")

01. @Target({PACKAGE, TYPE, METHOD, FIELD})
02. @Retention(RUNTIME)
03. public @interface GenericGenerator {
04. /\**
05. * unique generator name
06. \*/
07. String name();
08. /\**
09. * Generator strategy either a predefined Hibernate
10. * strategy or a fully qualified class name.
11. \*/
12. String strategy();
13. /\**
14. * Optional generator parameters
15. \*/
16. Parameter\[] parameters() default {};
17. }

name属性指定生成器名称。   
strategy属性指定具体生成器的类名。   
parameters得到strategy指定的具体生成器所用到的参数。 

对于这些hibernate主键生成策略和各自的具体生成器之间的关系,在org.hibernate.id.IdentifierGeneratorFactory中指定了, 

Java代码  [![收藏代码](http://xiaogui9317170.iteye.com/images/icon_star.png)](http://xiaogui9317170.iteye.com/blog/283526 "收藏这段代码")

01. static {
02. GENERATORS.put("uuid", UUIDHexGenerator.class);
03. GENERATORS.put("hilo", TableHiLoGenerator.class);
04. GENERATORS.put("assigned", Assigned.class);
05. GENERATORS.put("identity", IdentityGenerator.class);
06. GENERATORS.put("select", SelectGenerator.class);
07. GENERATORS.put("sequence", SequenceGenerator.class);
08. GENERATORS.put("seqhilo", SequenceHiLoGenerator.class);
09. GENERATORS.put("increment", IncrementGenerator.class);
10. GENERATORS.put("foreign", ForeignGenerator.class);
11. GENERATORS.put("guid", GUIDGenerator.class);
12. GENERATORS.put("uuid.hex", UUIDHexGenerator.class); //uuid.hex is deprecated
13. GENERATORS.put("sequence-identity", SequenceIdentityGenerator.class);
14. }

上面十二种策略，加上native，hibernate一共默认支持十三种生成策略。 

1、native 

Java代码  [![收藏代码](http://xiaogui9317170.iteye.com/images/icon_star.png)](http://xiaogui9317170.iteye.com/blog/283526 "收藏这段代码")

1. @GeneratedValue(generator = "paymentableGenerator")
2. @GenericGenerator(name = "paymentableGenerator", strategy = "native")

2、uuid 

Java代码  [![收藏代码](http://xiaogui9317170.iteye.com/images/icon_star.png)](http://xiaogui9317170.iteye.com/blog/283526 "收藏这段代码")

1. @GeneratedValue(generator = "paymentableGenerator")
2. @GenericGenerator(name = "paymentableGenerator", strategy = "uuid")

3、hilo 

Java代码  [![收藏代码](http://xiaogui9317170.iteye.com/images/icon_star.png)](http://xiaogui9317170.iteye.com/blog/283526 "收藏这段代码")

1. @GeneratedValue(generator = "paymentableGenerator")
2. @GenericGenerator(name = "paymentableGenerator", strategy = "hilo")

4、assigned 

Java代码  [![收藏代码](http://xiaogui9317170.iteye.com/images/icon_star.png)](http://xiaogui9317170.iteye.com/blog/283526 "收藏这段代码")

1. @GeneratedValue(generator = "paymentableGenerator")
2. @GenericGenerator(name = "paymentableGenerator", strategy = "assigned")

5、identity 

Java代码  [![收藏代码](http://xiaogui9317170.iteye.com/images/icon_star.png)](http://xiaogui9317170.iteye.com/blog/283526 "收藏这段代码")

1. @GeneratedValue(generator = "paymentableGenerator")
2. @GenericGenerator(name = "paymentableGenerator", strategy = "identity")

6、select 

Java代码  [![收藏代码](http://xiaogui9317170.iteye.com/images/icon_star.png)](http://xiaogui9317170.iteye.com/blog/283526 "收藏这段代码")

1. @GeneratedValue(generator = "paymentableGenerator")
2. @GenericGenerator(name="select", strategy="select",
3. parameters = { @Parameter(name = "key", value = "idstoerung") })

7、sequence 

Java代码  [![收藏代码](http://xiaogui9317170.iteye.com/images/icon_star.png)](http://xiaogui9317170.iteye.com/blog/283526 "收藏这段代码")

1. @GeneratedValue(generator = "paymentableGenerator")
2. @GenericGenerator(name = "paymentableGenerator", strategy = "sequence",
3. parameters = { @Parameter(name = "sequence", value = "seq\_payablemoney") })

8、seqhilo 

Java代码  [![收藏代码](http://xiaogui9317170.iteye.com/images/icon_star.png)](http://xiaogui9317170.iteye.com/blog/283526 "收藏这段代码")

1. @GeneratedValue(generator = "paymentableGenerator")
2. @GenericGenerator(name = "paymentableGenerator", strategy = "seqhilo",
3. parameters = { @Parameter(name = "max\_lo", value = "5") })

9、increment 

Java代码  [![收藏代码](http://xiaogui9317170.iteye.com/images/icon_star.png)](http://xiaogui9317170.iteye.com/blog/283526 "收藏这段代码")

1. @GeneratedValue(generator = "paymentableGenerator")
2. @GenericGenerator(name = "paymentableGenerator", strategy = "increment")

10、foreign 

Java代码  [![收藏代码](http://xiaogui9317170.iteye.com/images/icon_star.png)](http://xiaogui9317170.iteye.com/blog/283526 "收藏这段代码")

1. @GeneratedValue(generator = "idGenerator")
2. @GenericGenerator(name = "idGenerator", strategy = "foreign",
3. parameters = { @Parameter(name = "property", value = "employee") })

注意：直接使用@PrimaryKeyJoinColumn 报错（?） 

Java代码  [![收藏代码](http://xiaogui9317170.iteye.com/images/icon_star.png)](http://xiaogui9317170.iteye.com/blog/283526 "收藏这段代码")

1. @OneToOne(cascade = CascadeType.ALL)
2. @PrimaryKeyJoinColumn

例如 

Java代码  [![收藏代码](http://xiaogui9317170.iteye.com/images/icon_star.png)](http://xiaogui9317170.iteye.com/blog/283526 "收藏这段代码")

1. @Entity
2. public class Employee {
3. @Id Integer id;
4. @OneToOne @PrimaryKeyJoinColumn
5. EmployeeInfo info;
6. ...
7. }

应该为 

Java代码  [![收藏代码](http://xiaogui9317170.iteye.com/images/icon_star.png)](http://xiaogui9317170.iteye.com/blog/283526 "收藏这段代码")

01. @Entity
02. public class Employee {
03. @Id
04. @GeneratedValue(generator = "idGenerator")
05. @GenericGenerator(name = "idGenerator", strategy = "foreign",
06. parameters = { @Parameter(name = "property", value = "info") })
07. Integer id;
08. @OneToOne
09. EmployeeInfo info;
10. ...
11. }

11、guid 

Java代码  [![收藏代码](http://xiaogui9317170.iteye.com/images/icon_star.png)](http://xiaogui9317170.iteye.com/blog/283526 "收藏这段代码")

1. @GeneratedValue(generator = "paymentableGenerator")
2. @GenericGenerator(name = "paymentableGenerator", strategy = "guid")

12、uuid.hex 

Java代码  [![收藏代码](http://xiaogui9317170.iteye.com/images/icon_star.png)](http://xiaogui9317170.iteye.com/blog/283526 "收藏这段代码")

1. @GeneratedValue(generator = "paymentableGenerator")
2. @GenericGenerator(name = "paymentableGenerator", strategy = "uuid.hex")

13、sequence-identity 

Java代码  [![收藏代码](http://xiaogui9317170.iteye.com/images/icon_star.png)](http://xiaogui9317170.iteye.com/blog/283526 "收藏这段代码")

1. @GeneratedValue(generator = "paymentableGenerator")
2. @GenericGenerator(name = "paymentableGenerator", strategy = "sequence-identity",
3. parameters = { @Parameter(name = "sequence", value = "seq\_payablemoney") })

四、通过@GenericGenerator自定义主键生成策略   
如果实际应用中，主键策略为程序指定了就用程序指定的主键（assigned），没有指定就从sequence中取。   
明显上面所讨论的策略都不满足，只好自己扩展了，集成assigned和sequence两种策略。 

Java代码  [![收藏代码](http://xiaogui9317170.iteye.com/images/icon_star.png)](http://xiaogui9317170.iteye.com/blog/283526 "收藏这段代码")

01. public class AssignedSequenceGenerator extends SequenceGenerator implements
02. PersistentIdentifierGenerator, Configurable {
03. private String entityName;
04. public void configure(Type type, Properties params, Dialect dialect) throws MappingException {
05. entityName = params.getProperty(ENTITY\_NAME);
06. if (entityName==null) {
07. throw new MappingException("no entity name");
08. }
09. super.configure(type, params, dialect);
10. }
11. public Serializable generate(SessionImplementor session, Object obj)
12. throws HibernateException {
13. Serializable id = session.getEntityPersister( entityName, obj )
14. .getIdentifier( obj, session.getEntityMode() );
15. if (id==null) {
16. id = super.generate(session, obj);
17. }
18. return id;
19. }
20. }

实际应用中，定义同sequence。 

Java代码  [![收藏代码](http://xiaogui9317170.iteye.com/images/icon_star.png)](http://xiaogui9317170.iteye.com/blog/283526 "收藏这段代码")

1. @GeneratedValue(generator = "paymentableGenerator")
2. @GenericGenerator(name = "paymentableGenerator", strategy = "AssignedSequenceGenerator",
3. parameters = { @Parameter(name = "sequence", value = "seq\_payablemoney") })

 

 

 

四种数据库的支持情况如下：

 

数据库名称

支持的id策略

mysql

GenerationType.*TABLE*

GenerationType.*AUTO*

GenerationType.*IDENTITY*

*不支持*GenerationType.*SEQUENCE*

 

oracle

strategy=GenerationType.*AUTO*

GenerationType.*SEQUENCE*

GenerationType.*TABLE*

*不支持*GenerationType.*IDENTITY*

 

postgreSQL

GenerationType.*TABLE*

GenerationType.*AUTO*

GenerationType.*IDENTITY*

GenerationType.*SEQUENCE*

*都支持*

 

kingbase

GenerationType.*TABLE*

GenerationType.*SEQUENCE*

GenerationType.*IDENTITY*

GenerationType.*AUTO*

*都支持*