原文链接:http://blog.csdn.net/csuliky/article/details/5719304
1.介绍
Hibernate是目前最为常用的ORM框架,当然也有人会选择iBATIS或者其他的架构.这里我们着重讲Hibernate的ID生成策略
2.可选的ID策略生成方式
(1)native/auto
根据不同的数据库采用不同的ID生成方式,例如:在SQL Server中采用identity; 在MySQL中采用auto_increment; 在ORACLE中就会采用sequence, 注意hibernate会自动帮你创建一个名字叫hibernate_sequence的序列,不用自己去创建.这也是最常用和省事的.
例子:采用xml方式配置
- <hibernate-mapping>
- <class name="abu.csdn.bean.User" table="users">
- <id column="uid" name="uid" type="java.lang.Long">
- <generator class="native"/>
- </id>
- .....
- </class>
- <hibernate-mapping>
例子:采用注解方式(注意只要在id的getter上写上@Id就可以了,默认就是@GeneratedValue(strategy = GenerationType.AUTO), 另外(strategy = GenerationType.AUTO)也可以不写.)以下3种方式结果都是一样.
(a)
- @Entity(name = "users")
- public class User {
- private long uid;
- @Id
- @GeneratedValue(strategy = GenerationType.AUTO)
- public long getUid() {
- return uid;
- }
- public void setUid(long uid) {
- this.uid = uid;
- }
- }
(b)
- @Entity(name = "users")
- public class User {
- private long uid;
- @Id
- @GeneratedValue
- public long getUid() {
- return uid;
- }
- public void setUid(long uid) {
- this.uid = uid;
- }
- }
(c)
- @Entity(name = "users")
- public class User {
- private long uid;
- @Id
- public long getUid() {
- return uid;
- }
- public void setUid(long uid) {
- this.uid = uid;
- }
- }
(2)identity
这种策略在采用SQL Server时,相当于SQL Server的identity关键字, 不能用在Oracle中.
例子:采用xml配置方式
- <hibernate-mapping>
- <class name="abu.csdn.bean.User" table="users">
- <id column="uid" name="uid" type="java.lang.Long">
- <generator class="identity"/>
- </id>
- .....
- </class>
- <hibernate-mapping>
例子:采用注解方式
- @Entity(name = "users")
- public class User {
- private long uid;
- @Id
- @GeneratedValue(strategy = GenerationType.IDENTITY)
- public long getUid() {
- return uid;
- }
- public void setUid(long uid) {
- this.uid = uid;
- }
- }
(3)sequence
当然采用sequence的就是oracle了.生成方式开始已经说过了.如果要指定sequence的名字那么请看下面两个例子
例子:采用xml配置
- <hibernate-mapping>
- <class name="abu.csdn.bean.User" table="users">
- <id column="uid" name="uid" type="java.lang.Long">
- <generator class="sequence">
- <param name="sequence">csu_user_sequence</param> </generator>
- </id>
- .....
- </class>
- <hibernate-mapping>
例子:采用注解方式
- @Entity(name = "users")
- @SequenceGenerator(name = "userSEQ", sequenceName = "csu_user_sequence")
- public class User {
- private long uid;
- @Id
- @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "userSEQ")
- public long getUid() {
- return uid;
- }
- public void setUid(long uid) {
- this.uid = uid;
- }
- }
(4)table
使用一张数据表来管理所有的数据表的主键生成,这个最为麻烦,但是用得好会非常方便,尤其是大型项目,数据表非常多的时候.这里只举注解的例子,后说明
- @Entity
- @TableGenerator(name = "csdnGenerator", table = "csdn_generator", pkColumnName = "keyName", valueColumnName = "keyValue", pkColumnValue = "teacher", allocationSize = 1)
- public class Teacher implements Serializable {
- @Id
- @GeneratedValue(strategy = GenerationType.TABLE, generator = "sisTableGen")
- public long getTeaId() {
- return teaId;
- }
- public void setTeaId(long teaId) {
- this.teaId = teaId;
- }
- }
说明:
(a)@TableGenerator表示这个表要采用table的id生成方式,括号中的各个参数说明:
(i) name表示生成器的名字,在随后实体的id中要指明.
(ii)table表示在数据库中生成的管理id生成策略的表的名字.
(iii)pkColumnName表示主键的名字,也就是说记录每个表主键的名字
(iv)pkColumnValue表示主键的值,每个表主键的值
(v)valueColumnName表示这个表要记录的主键的名字,例如这里是teacher,那么在csdn_generator表中就用teacher来记录主键的值
(vi)allocationSize表示每次取得一个主键的值之后增长的步长值,这里是每次递增1.
(b)@GeneratedValue表示在主键中指明具体的id生成策略,,strategy表示要采用的ID生成策略,由于这里采用的是table,当然就是GenerationType.TABLE了,generator指出id生成器的名字,这里就是csdnGenerator.