標籤:abi oct int .hbm.xml ott type algorithm doc sequence
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資料庫。
@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建置原則配置