Hibernate的映射文件(hbm.xml)屬性說明

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操作時,是否包含本字段數據

http://hi.baidu.com/tianworking/blog/item/88268f1207083ccfc2fd7849.html

====================================================================

<?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讀取

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章