您的位置:首页 > 其它

hibernate常用映射配置总结

2017-02-17 00:03 141 查看
集合映射

<set name="" table="">

  <key column=""></key>

  <element column="" type=""></element>

</set>

<list name="" table="">

  <key column=""></key>

  <index column=""></index>

  <element column="" type=""></element>

</list>

<map name="" table="">

  <key column=""></key>

  <map-key column="" type=""></map-key>

  <element column="" type=""></element>

</map>

复合主键映射

  tips:复合主键一定要实现Serializable接口

<composite-id name="" class="复合主键所属类类">

  <key-property name="" column=""></key-property>

</composite-id>

一对多

<set name="" >

  <key column=""></key>

  <one-to-many class=""/>

</set>

多对一

<many-to-one name="" column="" class=""></many-to-one>

多对多(中间表实现)

<set name="集合属性的名字" table="中间表的名字">

  <key column="中间表引用的字段名(外键,对应当前配置表的主键)"></key>

  <many-to-many column="中间表外键对应的字段" class="集合属性的类型s"></many-to-many>

</set>

一对一

(1)通过外键映射

 无外建方

 <one-to-one name="" class="" property-ref=""></one-to-one>

 有外键方

 <many-to-one name="" column="" class="" unique="true 多对一并且外键为唯一值实现一对一" ></many-to-one>

 

(2)通过主键映射

  无主键方

   <one-to-one name="" class=""></one-to-one>

  有主键方

    <id name="">

      <generator class="foreign">
<param name="property">和下面的name要一样</param>
</generator>

    </id>

    <one-to-one name="" class="" constrained="true 添加外键约束"></one-to-one>

   

组合映射(类中包含类,映射到同一张表)

<component name="" class="">

  <property name="" type=""></property>

</component>

继承映射

(1)简单的继承映射

  每个子类都要有配置文件,然后通过<property></property>设定所有字段

  

 (2)复杂的继承映射

   (2.1)映射为一张表

    <!-- 鉴别器 -->
<discriminator column="" type=""></discriminator>

    <subclass name="" class="" discriminator-value="">

       <property name="" type=""></property>

    </subclass>

    (2.2)父类,子类都映射一张表

    <joined-subclass name="" table="">

          <key column=""/>

         <property name="" type=""></property>

    <joined-subclass>

    

    (2.2)每个子类都映射一张表

    <id name="">

      <generator class="不能为自增长主键生成器"></generator> 

    </id>

    <union-subclass name="" table="">

       <property name="" type=""></property>

    </union-subclass>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: