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_key | t_value |
user | 1 |
的增加方式还是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. 支持自定义生成策略