hibernate注解(annoation)使用id主键生成策略

阅读更多

参考原文:http://blog.csdn.net/shendeguang/article/details/47976117

hibernate注解(annoation)使用id主键生成主要要使用到以下几个注解: @Id,@GeneratedValue,@GenericGenerator. ,@GeneratedValue的作用是JPA的默认实现自定义主键生成策略,@GenericGenerator是hibernate在JPA的基础上增强。

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

比如说,JPA标准用法 

@Id
@GeneratedValue(GenerationType.AUTO)

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

@GeneratedValue(strategy = GenerationType.AUTO)
@GenericGenerator(name = "paymentableGenerator", strategy = "native")

 使用@GeneratedValue

1、针对mysql,oracle,sqlserver数据库可以同时使用

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@GenericGenerator(name = "paymentableGenerator", strategy = "native")

 name属性指定生成器名称。

strategy属性指定具体生成器的类名。

parameters得到strategy指定的具体生成器所用到的参数。

2、配合使用@GenericGenerator 不单独使用

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

//Constructs a new DefaultIdentifierGeneratorFactory.
public  DefaultIdentifierGeneratorFactory() {
        register( "uuid2", UUIDGenerator.class);
        register( "guid", GUIDGenerator.class);       
        register( "uuid", UUIDHexGenerator.class);        
        register( "uuid.hex", UUIDHexGenerator.class);    
        register( "hilo", TableHiLoGenerator.class);
        register( "assigned", Assigned.class);
        register( "identity", IdentityGenerator.class);
        register( "select", SelectGenerator.class);
        register( "sequence", SequenceGenerator.class);
        register( "seqhilo", SequenceHiLoGenerator.class);
        register( "increment", IncrementGenerator.class);
        register( "foreign", ForeignGenerator.class);
        register( "sequence-identity", SequenceIdentityGenerator.class);
        register( "enhanced-sequence", SequenceStyleGenerator.class);
        register( "enhanced-table", TableGenerator.class);
 }

如果想要不同的表使用相同的主键生成器,可以把他的generator的name属性设为相同即可,例如: 

@GeneratedValue(name="id1")
@GenericGenerator(name="id1",strategy="identity")

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

1、native 

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

2、uuid

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

3、hilo

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

4、assigned

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

5、identity

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

6、select 

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

7、sequence 

@GeneratedValue(generator = "paymentableGenerator")
@GenericGenerator(name = "paymentableGenerator", strategy = "sequence", parameters = { @Parameter(name = "sequence", value = "seq_payablemoney") })

8、seqhilo

@GeneratedValue(generator = "paymentableGenerator")
@GenericGenerator(name = "paymentableGenerator", strategy = "seqhilo", parameters = { @Parameter(name = "max_lo", value = "5") })

9、increment

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

你可能感兴趣的:(hibernate,注解,主键生成策略)