多对多关联映射 需要添加一张数据表,用来关联 双方

一,单向多对多关联映射

  1,在一端的bean类添加对方的属性

    如 Category类

      private Set<Item> items=new HashSet<Item>();

  2,修改该bean类对应的映射文件

    Category.hbm.xml

      <set name="items" table="CATEGORY_ITEM" >
        <key>
          <column name="C_ID" />
        </key>
        <many-to-many class="com.m01.n2n.Item" column="I_ID"></many-to-many>
      </set>

      name: 对应本类的属性 items; 

      table: 中间表; column的name : 指的是中间表中 指向本bean类的数据表的 外键

      class: 对面bean类的全命名;  column : 中间表中 指向对面bean类的数据表的 外键

      

 


 

一, 双向多对多关联

  1,在一端的bean类添加对方的属性

    如 Category类

      private Set<Item> items=new HashSet<Item>();

     Item类

      private Set<Category> categorys=new HashSet<>();

  2,修改bean类对应的映射文件 

    Category.hbm.xml 

      <set name="items" table="CATEGORY_ITEM" >
        <key>
          <column name="C_ID" />
        </key>
        <many-to-many class="com.m01.n2n.Item" column="I_ID"></many-to-many>
      </set>

      name: 对应本类的属性 items; 

      table: 中间表; column的name : 指的是中间表中 指向本bean类的数据表的 外键

      class: 对面bean类的全命名;  column : 中间表中 指向对面bean类的数据表的 外键

    Item.hbm.xml   

      <set name="categorys" table="CATEGORY_ITEM">
        <key>
          <column name="I_ID"></column>
        </key>
        <many-to-many class="com.m01.n2n.Category" column="C_ID"/>
      </set>

    同上;

 posted on 2017-02-10 01:19  夏末秋萍  阅读(107)  评论(0编辑  收藏  举报