Hibernate4主键生成策略(注解方式)

本文介绍了Hibernate中五种主键生成策略:IDENTITY、SEQUENCE、AUTO、TABLE及UUID,并通过示例展示了如何配置和使用这些策略。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

如果想使用hibernate增强的主键生成注解,需要设置hibernate.id.new_generator_mappings=true

1. IDENTITY自动增长,支持数据库有DB2MySQLMS SQL ServerSybaseHypersonicSQL

[html] view plaincopyprint?

1.  @Entity  

2.  public class User {  

3.      @Id  

4.      @GeneratedValue(strategy=GenerationType.IDENTITY)  

5.      private int id;  

6.      private String name;  

7.  ......getter & setter....  

8.  }  


2 SEQUENCE
序列,常用于oracle数据

[html] view plaincopyprint?

1.  @Entity  

2.  public class User {  

3.      @Id  

4.      @GeneratedValue(strategy=GenerationType.SEQUENCE)  

5.      private int id;  

6.      private String name;  

7.  ......getter & setter....  

8.  }  

@GeneratedValue中如果不指定generator具体名字,则由hibernate自动生成名为“hibernate_sequence”的序列.以下是指定数据库中序列名字为my_sequence.

[html] view plaincopyprint?

1.  @Entity  

2.  @SequenceGenerator(name="SEQ_GEN"sequenceName="my_sequence")  

3.  public class User {  

4.      @Id  

5.      @GeneratedValue(strategy=GenerationType.SEQUENCE,generator="SEQ_GEN")  

6.      private int id;  

7.      private String name;  

8.  ......getter & setter....  

9.  }  


也可以为序列指定开始值,增长值:

[html] view plaincopyprint?

1.  initialValue: the value from which the id is to start generating  

2.    

3.    

4.  allocationSize: the amount to increment by when allocating id numbers from the generator  


3. AUTO: 
根据底层数据库,选择IDENTITYSEQUENCE4. TABLE: 生成一张表来存储生成的主键信息

[html] view plaincopyprint?

1.  pkColumnName: the column name containing the entity identifier  

2.    

3.  valueColumnName: the column name containing the identifier value  

4.    

5.  pkColumnValue: the entity identifier  

6.    

7.  uniqueConstraints: any potential column constraint on the table containing the ids  

 

其中:  name指定生成策略的别名,table是存放主键生成的表名,pkColumnName:为字段名字,valueColumnName为字段值,pkColumnValue为具体字段对应的值pkColumnName可以理解为Map中的key, valueColumnNameMap中的value,pkColumnValue具体的Key内容。

[html] view plaincopyprint?

1.  @Entity  

2.  @Table(name = "t_user")  

3.  @TableGenerator(name = "USER_GEN",   //别名  

4.      table = "GENERATOR_TABLE",       //生成的表名  

5.      pkColumnName = "t_key",          //key列名  

6.      valueColumnName = "t_value",     //value列名    

7.      pkColumnValue = "user",          //具体key内容  

8.      initialValue=10,                 //初始值  

9.      allocationSize = 20)             //增长值  

10. public class User {  

11.     @Id  

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

13.     private int id;  

14.     private String name;  

15. ......getter & setter....  

16. }  

 

[html] view plaincopyprint?

1.  测试示例:     

2.  @Test  

3.  public void testUser1(){  

4.      Configuration configuration = new Configuration().configure();     

5.      ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry();             

6.      SessionFactory sessionFactory = configuration.buildSessionFactory(serviceRegistry);   

7.      Session session = sessionFactory.getCurrentSession();  

8.      session.beginTransaction();  

9.      User user = new User();  

10.     user.setName("zhangsan");  

11.     session.save(user);  

12.     session.getTransaction().commit();  

13. }  

具体生成的表如下:

t_key

t_value

user

1

 

 

PS: 如果是Mysql,发现一个问题,即使设置了initialValue值也是无效的,allocationSize这个值有设置的话,user表主键为12040这样,但GENERATOR_TABLE
的增加方式还是123,有兴趣的可以试试,oracle数据库未测试。
5. 还有一种uuid的生成策略:

[html] view plaincopyprint?

1.  @Entity  

2.  public class Person {  

3.      @Id  

4.      @GeneratedValue(generator = "system-uuid")  

5.      @GenericGenerator(name = "system-uuid"strategy = "uuid")  

6.      private String id;  

7.      private String name;  

8.  .....getter & setter....  

9.  }  


6. 
支持自定义生成策略

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值