Hibernate的映射檔案(hbm.xml)屬性說明
1.class 節點
name: 類名
table: 類對應表名,預設為類名稱
dynamic-update: 生成更新字段時,隻包含發生變動的字段,預設為false。
dynamic-insert: 生成insert語句時僅包含非null字段
Proxy: 代理類,預設為空
discriminator-value: 子類辨識辨別用于多态支援
where: 通過限定條件查詢結果集。如:查詢有籍在校學生的資訊可以使用"where studentstatus='0'"
2.id節點
1.column 字段名稱
2.type 字段類型
3.length 字段長度
4.unsaved-value 用于判斷對象值是否已經儲存
5.generator-class 主鍵産生方式
assigned
hilo
seqhilo
increment
identity
sequence
native
uuid.hex
uuid.string
foreign
---------------------------------------------------------------------------------------------------------------
主鍵産生方式說明
increment(遞增)
用于為long, short或者int類型生成唯一辨別。隻有在沒有其他程序往同一張表中插入資料時才能使用。在叢集下不要使用。
identity
對DB2,MySQL, MS SQL Server, Sybase和HypersonicSQL的内置辨別字段提供支援。傳回的辨別符是long, short 或者int類型的。
sequence (序列)
在DB2,PostgreSQL, Oracle, SAP DB, McKoi中使用序列(sequence),而在Interbase中使用生成器(generator)。傳回的辨別符是long, short或者 int類型的。
hilo (高低位)
使用一個高/低位算法來高效的生成long, short或者 int類型的辨別符。給定一個表和字段(預設分别是是hibernate_unique_key 和next_hi)作為高位值得來源。高/低位算法生成的辨別符隻在一個特定的資料庫中是唯一的。在使用JTA獲得的連接配接或者使用者自行提供的連接配接中,不要使用這種生成器。
seqhilo(使用序列的高低位)
使用一個高/低位算法來高效的生成long, short或者 int類型的辨別符,給定一個資料庫序列(sequence)的名字。
uuid.hex
用一個128-bit的UUID算法生成字元串類型的辨別符。在一個網絡中唯一(使用了IP位址)。UUID被編碼為一個32位16進制數字的字元串。
uuid.string
使用同樣的UUID算法。UUID被編碼為一個16個字元長的任意ASCII字元組成的字元串。不能使用在PostgreSQL資料庫中
native(本地)
根據底層資料庫的能力選擇identity, sequence 或者hilo中的一個。
assigned(程式設定)
讓應用程式在save()之前為對象配置設定一個标示符。
foreign(外部引用)
-------------------------------------------------------------------------------------------------------------------------
3.property 節點
1.column 資料庫表字段名稱
2.type 類型
3.length 長度
4.not-null 字段是否允許為空
5.unique 字段是否允許唯一(是否允許重複值)
6.insert insert操作時,是否允許包含本字段數值
7.update update操作時,是否包含本字段資料
====================================================================
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping>
<class name="com.oreilly.hh.Track" table="TRACK">
<meta attribute="class-description">
Represents a single playable track in the music database.
@author Jim Elliot(with help from Hibernate)
</meta>
<id name="id" type="int" column="TRACK_ID">
<meta attribute="scope-set">protected</meta>
<generator class="native"/>
</id>
<property name="title" type="string" not-null="true"/>
<property name="filePath" type="string" not-null="true"/>
<property name="playTime" type="time">
<meta attribute="field-description">Playing time</meta>
</property>
<property name="added" type="date">
<meta attribute="field-description">When the track was created</meta>
</property>
<property name="volume" type="short" not-null="true">
<meta attribute="field-description">How loud to play the track</meta>
</property>
</class>
</hibernate-mapping>
說明如下:
1.<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//hibernate/Hibernate Mapping DTD 2.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping>
用于導言說明,說明它的檔案格式定義。
2.<hibernate-mapping>标簽裡是真正的映射。
3.<class name="com.oreilly.hh.Track" table="TRACK">
定義一個類com.oreilly.hh.Track的映射。(可以定義任意多個類在一個映射檔案裡)。表示存在資料庫表TRACK中。
4. <meta attribute="class-description">
Represents a single playable track in the music database.
@author Jim Elliot(with help from Hibernate)
</meta>
定義了說明,可以被JavaDoc讀取。
5. <id name="id" type="int" column="TRACK_ID">
<meta attribute="scope-set">protected</meta>
<generator class="native"/>
</id>
定義了類屬性和資料庫表列的映射。 <generator class="native"/>是表示ID生成政策,此種政策有多種。
6. <property name="volume" type="short" not-null="true">
<meta attribute="field-description">How loud to play the track</meta>
</property> 定義了說明,可以被JavaDoc讀取。