hibernate框架学习要点(二)

来源:互联网 发布:阿卡索英语 知乎 编辑:程序博客网 时间:2024/06/05 08:44

深入hibernate映射文件

1.映射主键`:用id属性映射主键,大部分数据库都提供了医用的主键生成机制(identity字段或sequence等),采用的主键生成方式即可。

                <id name="id" type="int">                <generator class="native"/>                </id>`

2.映射普通属性:<property name=""></property>
3.映射集合属性(持久化类的属性有集合类型时):注意集合属性中的数据元素都会被映射到另一个表中,所以集合中要映射另一个表的外键
(一)List集合

    <list name="list" table="" >        //映射另一个表的外键        <key column="" not-null="true"></key>        //映射集合属性表的索引列        <list-index column=""></list-index>        //映射保存数组元素的列        <element column="" type=""></element>    </list>

(二)Set集合

    <set name="" table="" >        <key column="" not-null="true"></key>        <element column="" type=""></element>    </set>

(三)数组

    <array name="set" table="" >        <key column="" not-null="true"></key>        <list-index column=""></list-index>        <element column="" type=""></element>    </array>

(四)Map集合

     <map name="" table="">        <key column="" not-null="true"></key>        <map-key type="" column=""></map-key>        <element not-null="" column=""></element>    </map>

(五)映射各种Collection集合,但都会被映射成无序集合

    <bag name="" table="" >        <key column="" not-null="true"></key>        <element column="" type=""></element>    </bag>

4.映射组件属性(持久化类中是复合类型的属性就是组件属性):

   <list name="list" table="user_infor" >        <key column="people_id" not-null="true"></key>        <list-index column="id"></list-index>        <element column="position" type="string"></element>        <component name="" class="">            <parent name=""></parent>            <property></property>        </component>    </list>

当组件中有集合属性时:

        <list name="" table="" >            <key column="" not-null=""></key>            <list-index column=""></list-index>            <element column="" type=""></element>            <component name="" class="">                <parent name=""></parent>                <property></property>                <set name="" table="" >                    <key column="" not-null="true"></key>                    <element column="" type=""></element>                </set>            </component>        </list>

注:组件属性中需要包含一个owner属性,用于指向包含该属性类的对象

5.映射集合属性(集合属性的元素为组件时)

     <set name="" table="" >        <key column="" not-null="true"></key>        <element column="" type=""></element>        <composite-element class="">            <parent name=""></parent>            <property></property>        </composite-element>             </set>