zoukankan      html  css  js  c++  java
  • hibernate 多对多关联映射

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

    一,单向多对多关联映射

      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>

        同上;

  • 相关阅读:
    玩具数据库
    数据库中可能会出现的表
    SqlDbHelper
    重写 覆盖 虚方法
    页面标签的初始化
    如何将UTF8转换为UTF8n
    小软件项目开发的管理(转)
    SCRUM软件开发过程(转)
    在.Net如何制作自定义的快捷方式
    What Is a Leader
  • 原文地址:https://www.cnblogs.com/m01qiuping/p/6384782.html
Copyright © 2011-2022 走看看