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

IDENTITY: supports identity columns in DB2, MySQL, MS SQL Server, Sybase and HypersonicSQL. The returned identifier is of type long, short or int.

SEQUENCE (called seqhilo in Hibernate): uses a hi/lo algorithm to efficiently generate identifiers of type long, short or int, given a named database sequence.

TABLE (called MultipleHiLoPerTableGenerator in Hibernate) : uses a hi/lo algorithm to efficiently generate identifiers of type long, short or int, given a table and column as a source of hi values.

The hi/lo algorithm generates identifiers that are unique only for a particular database.

AUTO: selects IDENTITY, SEQUENCE or TABLE depending upon the capabilities of the underlying database.

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

1. IDENTITY: 自动增长,支持数据库有DB2、MySQL、MS SQL Server、Sybase、HypersonicSQL

@Entity
public class User {
	@Id
	@GeneratedValue(strategy=GenerationType.IDENTITY)
	private int id;
	private String name;
......getter & setter....
}

2 SEQUENCE: 序列,常用于oracle数据
@Entity
public class User {
	@Id
	@GeneratedValue(strategy=GenerationType.SEQUENCE)
	private int id;
	private String name;
......getter & setter....
}
@GeneratedValue中如果不指定generator具体名字,则由hibernate自动生成名为“hibernate_sequence”的序列.以下是指定数据库中序列名字为my_sequence.
@Entity
@SequenceGenerator(name="SEQ_GEN", sequenceName="my_sequence")
public class User {
	@Id
	@GeneratedValue(strategy=GenerationType.SEQUENCE,generator="SEQ_GEN")
	private int id;
	private String name;
......getter & setter....
}

也可以为序列指定开始值,增长值:
initialValue: the value from which the id is to start generating


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

3. AUTO: 根据底层数据库,选择IDENTITY或SEQUENCE4. TABLE: 生成一张表来存储生成的主键信息
pkColumnName: the column name containing the entity identifier

valueColumnName: the column name containing the identifier value

pkColumnValue: the entity identifier

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


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

@Entity
@Table(name = "t_user")
@TableGenerator(name = "USER_GEN",   //别名
	table = "GENERATOR_TABLE",    	 //生成的表名
	pkColumnName = "t_key", 	  	 //key列名
	valueColumnName = "t_value",  	 //value列名  
	pkColumnValue = "user", 		 //具体key内容
	initialValue=10,				 //初始值
	allocationSize = 20)			 //增长值
public class User {
	@Id
	@GeneratedValue(strategy = GenerationType.TABLE, generator="USER_GEN")
	private int id;
	private String name;
......getter & setter....
}

测试示例:	
@Test
public void testUser1(){
	Configuration configuration = new Configuration().configure();   
	ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry();           
	SessionFactory sessionFactory = configuration.buildSessionFactory(serviceRegistry); 
	Session session = sessionFactory.getCurrentSession();
	session.beginTransaction();
	User user = new User();
	user.setName("zhangsan");
	session.save(user);
	session.getTransaction().commit();
}
具体生成的表如下:
t_keyt_value
user1
  
PS: 如果是Mysql,发现一个问题,即使设置了initialValue值也是无效的,allocationSize这个值有设置的话,user表主键为1、20、40这样,但 GENERATOR_TABLE
的增加方式还是1、2、3,有兴趣的可以试试,oracle数据库未测试。
5. 还有一种uuid的生成策略:
@Entity
public class Person {
	@Id
	@GeneratedValue(generator = "system-uuid")
	@GenericGenerator(name = "system-uuid", strategy = "uuid")
	private String id;
	private String name;
.....getter & setter....
}

6. 支持自定义生成策略
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值