Hibernate学习笔记(八)【映射实体关系】

本文只简略的介绍关系,详细标签描述参考http://blog.csdn.net/tanxiang21/article/details/8034105

一对一

Monkey.hbm.xml

<one-to-one name="address" 
        class="mypack.Address"
        cascade="all" 
     />

Address.hbm.xml

<class name="mypack.Address" table="ADDRESSES" >
    <id name="id" type="long" column="ID">
      <generator class="foreign">
        <param name="property">monkey</param>
      </generator>
    </id>
    <one-to-one name="monkey" 
        class="mypack.Monkey"
       constrained="true"
    />
</class>

说明:Address constrained="true" generator class="foreign"

ADDRESSES表ID主键做为外键,两表表共享OID

显然,Monkey 与Address存在主从关系。

数据库一对一,还有映射继承关系的 参考http://blog.csdn.net/tanxiang21/article/details/8045851

多对多

Monkey 和Teacher是多对多,Learning是中间表

【组件型式】多对多形式:

<hibernate-mapping >
  <class name="mypack.Monkey" table="MONKEYS" >
    <id name="id" type="long" column="ID">
      <generator class="increment"/>
    </id>
    <property name="name" column="NAME" type="string" />  
    <set name="learnings" lazy="true" table="LEARNING" >
        <key column="MONKEY_ID" />
        <composite-element class="mypack.Learning" >
          <parent name="monkey" />
          <many-to-one name="teacher" class="mypack.Teacher" column="TEACHER_ID" not-null="true"/>
          <property name="gongfu" column="GONGFU" type="string" not-null="true" />
         </composite-element> 
    </set>     
  </class>
</hibernate-mapping>

说明:中间表LEARNING映射Learning类以集合组建形式属于Monkey类,然后和Teacher存在多对一的关联

Learning类共享Monkey类OID,操作时候一样是相互保存双方,同通常的多对多保存方式。

<composite>标签 和 组成关系<component>标签需区分,映射组成关系:http://blog.csdn.net/tanxiang21/article/details/8130201

<composite>标签 和 值类型集合<set>标签的区分是 里面不是<element> ,映射值类型集合http://blog.csdn.net/tanxiang21/article/details/8134107

【标准】多对多 数据库有中间表,中间表无对应BEAN

<hibernate-mapping >
  <class name="mypack.Monkey" table="MONKEYS" >
    <id name="id" type="long" column="ID">
      <generator class="increment"/>
    </id>
    <property name="name" column="NAME" type="string" />      
    <set name="teachers" table="LEARNING"
        lazy="true"
        cascade="save-update">
        <key column="MONKEY_ID" />
        <many-to-many class="mypack.Teacher" column="TEACHER_ID" />
    </set>       
  </class>
</hibernate-mapping>
<hibernate-mapping >
  <class name="mypack.Teacher" table="TEACHERS" >
    <id name="id" type="long" column="ID">
      <generator class="increment"/>
    </id>
    <property name="name" column="NAME" type="string" />   
    <set name="monkeys" table="LEARNING"
        lazy="true"
        inverse="true"
        cascade="save-update">
        <key column="TEACHER_ID" />
        <many-to-many class="mypack.Monkey" column="MONKEY_ID" />
    </set> 
  </class>
</hibernate-mapping>

说明:标准版得多对多,然后JAVA保存相互关联关系时,两边都要设置对方,

注意下inverse,而数据库只发一条修改语句

中间表做BEAN,中间可做个关系属性 一对多 多对一

<hibernate-mapping >
  <class name="mypack.Monkey" table="MONKEYS" >
    <id name="id" type="long" column="ID">
      <generator class="increment"/>
    </id>
   <property name="name" column="NAME" type="string" />   
   <set name="learnings" lazy="true" inverse="true" cascade="save-update">
        <key column="MONKEY_ID" />
        <one-to-many  class="mypack.Learning" />
   </set>   
  </class>
</hibernate-mapping>
<hibernate-mapping >
  <class name="mypack.Learning" table="LEARNING" >
    <id name="id" type="long" column="ID">
      <generator class="increment"/>
    </id>
    <property name="gongfu" column="GONGFU" type="string" />    
    <many-to-one name="monkey" column="MONKEY_ID" class="mypack.Monkey" not-null="true" />
    <many-to-one name="teacher" column="TEACHER_ID" class="mypack.Teacher" not-null="true" /> 
  </class>
</hibernate-mapping>
<hibernate-mapping >
  <class name="mypack.Teacher" table="TEACHERS" >
    <id name="id" type="long" column="ID">
      <generator class="increment"/>
    </id>
    <property name="name" column="NAME" type="string" />  
     <set name="learnings" lazy="true" inverse="true" cascade="save-update">
        <key column="TEACHER_ID" />
        <one-to-many  class="mypack.Learning" />
     </set> 
  </class>
</hibernate-mapping>

说明:其实就是多对多,变为了 一对多 多对一 ,注意可以级联保存,无限级联~ 无限级联~备注:文章示例代码来源:《Hibernate逍遥游记》孙卫琴,可能有部分改造,解释部分纯属个人经验。

相关推荐