hibernate ID生成策略配置

1.Student.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 identitysequence or hilo depending upon the capabilities of the underlying database.


2.Annotation配置_IDENTITY_SEQUENCE

@Id
	@GeneratedValue(strategy=GenerationType.IDENTITY)
	public int getId() {
		return id;
	}


Sequence生成器,使用oracle数据库。

@SequenceGeneratorname=”teacherSEQ”,sequenceName=”teacherSEQ_DB”)加在类前

 

@GeneratedValuestrategy=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>


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值