1. 程式人生 > >hibernate ID生成策略配置

hibernate ID生成策略配置

名稱 hiberna servle pop snippet false padding table varchar

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 identity, sequence 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>


hibernate ID生成策略配置