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

来源:互联网 发布:先知电子狗升级软件 编辑:程序博客网 时间:2024/06/05 09:00
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

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

2 SEQUENCE: 序列,常用于oracle数据
@Entitypublic 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 generatingallocationSize: the amount to increment by when allocating id numbers from the generator

3. AUTO: 根据底层数据库,选择IDENTITY或SEQUENCE4. TABLE: 生成一张表来存储生成的主键信息
pkColumnName: the column name containing the entity identifiervalueColumnName: the column name containing the identifier valuepkColumnValue: the entity identifieruniqueConstraints: 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....}

测试示例:@Testpublic 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_valueuser1  PS: 如果是Mysql,发现一个问题,即使设置了initialValue值也是无效的,allocationSize这个值有设置的话,user表主键为1、20、40这样,但GENERATOR_TABLE
的增加方式还是1、2、3,有兴趣的可以试试,oracle数据库未测试。
5. 还有一种uuid的生成策略:
@Entitypublic class Person {@Id@GeneratedValue(generator = "system-uuid")@GenericGenerator(name = "system-uuid", strategy = "uuid")private String id;private String name;.....getter & setter....}

6. 支持自定义生成策略
原创粉丝点击