多对多关联映射(映射文件)


<many-to-many .../>元素属性:


 class:指定关联实体的类名,默认由hibernate通过反射来获取该类名。

 not-found:该属性指定当外键参照的主记录表不存在时如何处理。接受ignoreexception两个值

 frmula:指定一个sql表达式。

 outer-join:指定Hibernate是否要启动外连接来抓取关联实体,该属性只能是true,falseauto三个值之一,其中true表示启用,false表示不启用,auto表示由程序来决定。

 fetch:该属性指定Hibernate的抓取策略,该属性值只能是Joinselect

 lazy:指定hibernate是否需要启动延迟加载来加载关联实体。

 unique:指定本持久化实体是否增加唯一约束,默认是false.

 where:该属性指定一个SQL表达式,指定当查询、获取关联实体时的过滤条件,只有满足where条件的关联实体才会被加载。

 order-by:该属性用于设置数据库对集合元素排序,该属性仅对1.4或更高版本的JDK有效。

 property-ref:指定关联类的一个属性,这个属性将会和本类的瓦见相对应(当外键参照唯一键时需指定该属性)。如果没有指定,直接使用对方关联类的主键。


多对多单向关联映射


1、建立t_orders和t_items表

    create table if not exists webdb.t_orders(

         id int(11) not null,

         order_no varchar(10) not null,

          money decimal(10,2) not null,

         PRIMARY key (id),

          Unique  key address_id (address_id)

     )ENGINE=InnoDB  DEFAULT CHARSET=utf-8 COLLATE=utf8_unicode_ci;


     create table if not exists webdb.t_items(

          id int(11) not null,

          item_no varchar(50),

          item_name varchar(60),

          PRIMARY key (id)

      )ENGINE=InnoDB  DEFAULT CHARSET=utf-8 COLLATE=utf8_unicode_ci;  

   

create table if not exists webdb.t_orders_items(     

          order_id  int(11) not null,

          item_id  int(11)  not null,

          PRIMARY key (order_id,item_id),

          CONSTRAINT ‘FK_orders_itmes’ FOREIGN KEY (`order_id`) REFERENCES `tb_orders` (`id`),

            CONSTRAINT `FK_orders_items_items` FOREIGN KEY (`item_id`) REFERENCES `tb_items` (`id`)

      )ENGINE=InnoDB  DEFAULT CHARSET=utf-8 COLLATE=utf8_unicode_ci;


2、对应的实体

   public class Orders{

      private Integer id;

      private String orderno;

      private Double  money;

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

       . .  .

}


    public class Items{

      private Integer id;

      private String itemno;

      private String itemname;

      .  .  .

 }

3、对应的映射文件配置:

   Order.hbm.xml:

   <class name="org.hibernate.test.Order"  table="t_orders">

        <id name="id" column="id" type="int">

            <generator class="native"/>

        </id>

        <property name="orderno" column="order_no" type="string"/>

         <property name="money" column="money" type="double"/>

        <!-- 映射OrdersItems的多对多单向关联-->

         <set name="items" cascade="save-update,delete"  lazy="true" table=t_orders_items”>

                 <key column="order_id"/>

                 <one-to-many class="org.hibernate.test.Items" column=itemid/>

          </set>


  </class>

Item.hbm.xml

<class name="org.hibernate.test.Items" table="t_items">

        <id name="id" column="id" type="int">

            <generator class="intiva/>

        </id>

        <property name="orderno" column="item_no" type="sring" />

         <property name="itemname" column="item_name" type="sring" />   

</class>


多对多双向关联映射

1、建立t_orders和t_items表

    create table if not exists webdb.t_orders(

         id int(11) not null,

         order_no varchar(10) not null,

          money decimal(10,2) not null,

         PRIMARY key (id),

          Unique  key address_id (address_id)

     )ENGINE=InnoDB  DEFAULT CHARSET=utf-8 COLLATE=utf8_unicode_ci;


     create table if not exists webdb.t_items(

          id int(11) not null,

          item_no varchar(50),

          item_name varchar(60),

          PRIMARY key (id)

      )ENGINE=InnoDB  DEFAULT CHARSET=utf-8 COLLATE=utf8_unicode_ci;  

   

create table if not exists webdb.t_orders_items(     

          order_id  int(11) not null,

          item_id  int(11)  not null,

          PRIMARY key (order_id,item_id),

          CONSTRAINT ‘FK_orders_itmes’ FOREIGN KEY (`order_id`) REFERENCES `tb_orders` (`id`),

            CONSTRAINT `FK_orders_items_items` FOREIGN KEY (`item_id`) REFERENCES `tb_items` (`id`)

      )ENGINE=InnoDB  DEFAULT CHARSET=utf-8 COLLATE=utf8_unicode_ci;


2、对应的实体

   public class Orders{

       private Integer id;

       private String orderno;

       private Double  money;

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

       . .  .

}


    public class Items{

       private Integer id;

       private String itemno;

       private String itemname;

       private Set<Orders> orders= new HashSet<Orders>();

      .  .  .

 }

3、对应的映射文件配置:

   Order.hbm.xml:

   <class name="org.hibernate.test.Order"  table="t_orders">

        <id name="id" column="id" type="int">

            <generator class="native"/>

        </id>

        <property name="orderno" column="order_no" type="string"/>

         <property name="money" column="money" type="double"/>

        <!-- 映射OrdersItems的多对多单向关联-->

       <set name="items" cascade="save-update,delete"  lazy="true" table=t_orders_items”>

                 <key column="order_id"/>

                 <many-to-many class="org.hibernate.test.Items" column=itemid/>

       </set>

  </class>

Item.hbm.xml

<class name="org.hibernate.test.Items" table="t_items">

        <id name="id" column="id" type="int">

                <generator class="intiva/>

        </id>

    <property name="orderno" column="item_no" type="sring" />

     <property name="itemname" column="item_name" type="sring" />   

       <set name="orders" cascade="save-update" inverse=true” lazy="true"  table=t_orders_items”>

                 <key column="item_id"/>

                 <many-to-many class="org.hibernate.test.Orders" column=order_id/>

       </set>

</class>