hibernate ID生成策略配置
2017-06-19 20:01
459 查看
1.Student.hbm.xml配置
uuid
uses a 128-bit UUID algorithm to generate identifiers of type string that are unique within a network (the IP address is used). The UUID is encoded as a string of 32 hexadecimal digits in length.
id的类型要为String类型。终于在mysql中产生的id为varchar类型。
selects
upon the capabilities of the underlying database.
2.Annotation配置_IDENTITY_SEQUENCE
Sequence生成器,使用oracle数据库。
@SequenceGenerator(name=”teacherSEQ”,sequenceName=”teacherSEQ_DB”)加在类前
@GeneratedValue(strategy=GenerationType.SEQUENCE,generator=”teacherSEQ”)加在方法前
3.TableGenerator(跨数据库平台)
@javax.persistence.TableGenerator(
name="Teacher_GEN",
table="GENERATOR_TABLE",
pkColumnName="pk_key",
valueColumnName="pk_value",
pkColumnValue="Teacher",
allocationSize=1
)
Student.hbm.xml
Teacher.hbm.xml
<hibernate-mapping package="com.wxh.hibernate.model"> <class name="Student" > <id name="id"> <generator class="uuid"></generator> </id> <property name="age"></property> <property name="name"></property> </class> </hibernate-mapping>
uuid
uses a 128-bit UUID algorithm to generate identifiers of type string that are unique within a network (the IP address is used). The UUID is encoded as a string of 32 hexadecimal digits in length.
id的类型要为String类型。终于在mysql中产生的id为varchar类型。
<hibernate-mapping package="com.wxh.hibernate.model"> <class name="Student" > <id name="id"> <generator class="uuid"></generator> </id> <property name="age"></property> <property name="name"></property> </class> </hibernate-mapping>
native
selects
identity,
sequenceor
hilodepending
upon the capabilities of the underlying database.
2.Annotation配置_IDENTITY_SEQUENCE
@Id @GeneratedValue(strategy=GenerationType.IDENTITY) public int getId() { return id; }
Sequence生成器,使用oracle数据库。
@SequenceGenerator(name=”teacherSEQ”,sequenceName=”teacherSEQ_DB”)加在类前
@GeneratedValue(strategy=GenerationType.SEQUENCE,generator=”teacherSEQ”)加在方法前
3.TableGenerator(跨数据库平台)
@javax.persistence.TableGenerator(
name="Teacher_GEN",
table="GENERATOR_TABLE",
pkColumnName="pk_key",
valueColumnName="pk_value",
pkColumnValue="Teacher",
allocationSize=1
)
Student.hbm.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="com.wxh.pojo"> <!-- name相应类名称 --> <class name="Student" > <!-- name相应属性名字 --> <id name="stuNo"> <!-- ID生成策略:针对不同的数据库对象使用不同的id生成方式 identity:mysql,mssqlservler(整型列) sequence:oracle,db2(整型列) uuid:一般用于字符串类型的主键 guid:同上(一般用于mysql,sqlserver) native:经常使用配置(依据不同的数据库自己主动选择使用identity,sequence。hilo中的当中一个作为生成策略) --> <generator class="native"> <!-- 设置自己定义序列的名字 --> <param name="sequence">auto_sid</param> </generator> </id> <property name="name" column="stuname" length="16" not-null="true" unique="false"> </property> <property name="major" length="32"></property> <property name="comingYear"></property> <property name="type" length="16"></property> </class> </hibernate-mapping>
Teacher.hbm.xml
<?xml version="1.0" encoding="UTF-8"? > <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="com.wxh.pojo"> <!-- name相应类名称 --> <class name="Teacher" > <!-- name相应属性名字 --> <id name="tid"> <!-- ID生成策略:针对不同的数据库对象使用不同的id生成方式 identity:mysql,mssqlservler(整型列) sequence:oracle,db2(整型列) uuid:一般用于字符串类型的主键 guid:同上(一般用于mysql,sqlserver) native:经常使用配置(依据不同的数据库自己主动选择使用identity,sequence,hilo中的当中一个作为生成策略) --> <generator class="native"> <!-- 设置自己定义序列的名字 --> <param name="sequence">auto_tid</param> </generator> </id> <property name="tname" length="16" not-null="true" unique="false"> </property> </class> </hibernate-mapping>
相关文章推荐
- Hibernate配置及三种Id生成策略
- hibernate ID生成策略配置
- SS6.2_Hibernate_Hibernate_IdStrategyAnnotation 主键生成策略uuid Annotation注解的配置方式
- SS6.1_Hibernate_Hibernate_IdStrategyAnnotation 主键生成策略native Annotation注解的配置方式
- hibernate id 生成策略 annotation @GeneratedValue
- 使用hibernate的id自动生成策略问题
- 关于Hibernate的oracle主键id生成策略uuid、native和sequence
- Hibernate保存记录时遇到的一个问题----evitic,not-null配置及主键生成策略
- hibernate id设置生成策略
- Hibernate中id的table生成策略之联合主键
- Hibernate各种主键生成策略与配置详解
- ID的生成策略(hibernate的id生成策略,主键类为什么需要实现序列化接口,同时还要重写hashCode()和equals()方法)
- Hibernate的ID生成策略/联合主键解决方案
- 关于Hibernate的oracle主键id生成策略uuid、native和sequence
- hibernate ID生成策略(sequence)
- 【Hibernate框架开发之四】Hibernate-Annotation常用的注解归总&&ID的生成策略&&联合主键
- Hibernate ID 生成策略
- hibernate的ID生成策略(annotation方式@GeneratedValue)
- hibernate id 生成策略
- Hibernate各种主键生成策略与配置详解