hibernate ID建置原則配置

來源:互聯網
上載者:User

標籤: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建置原則配置

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.