如果想使用hibernate增强的主键生成注解,需要设置hibernate.id.new_generator_mappings=true
1. IDENTITY:自动增长,支持数据库有DB2、MySQL、MS SQL Server、Sybase、HypersonicSQL
[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: 根据底层数据库,选择IDENTITY或SEQUENCE4. 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, valueColumnName为Map中的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表主键为1、20、40这样,但GENERATOR_TABLE
的增加方式还是1、2、3,有兴趣的可以试试,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. 支持自定义生成策略