Hibernate学习笔记:多对多的关联关系(many-to-many)


多对多的关系现实中也有很多, 学生选课程,订单和产品,老师和学生。
继续复用前面的类Order,新加一个Product类, 并以订单和产品的关系来分析多对对关系。多对多表现的比较特殊的地方是需要中间表,我们已经有了order(订单)表,添加一个product(产品)表,为了实现order(订单)和product(产品)的多对多关系, 还需要一个中间表,叫做order_line(暂且就叫做订单流水线表),当用户选择产品并且确定一个订单后,除了在order表添加一条记录以外,还会在order_line中添加n条记录来表明这一个订单关联了多个产品, 同样, 多个用户下的多个订单中, 都可能包含某一个产品, 这样就形成了多对多的关系. 需要作这么几个工作:添加一个Product对象,它包含一个Set,set里面放与之关联的Order;修改Order.java,添加一个属性products,用来持有与之关联的Product; 修改映射文件。

Order.java:

view plaincopy to clipboardprint?
public class Order {  
    private int id;  
    private float totalPrice;  
    private Member member;  
    private Set products;  
    //getters and setters ignored  
public String toString(){//}  

public class Order {
 private int id;
 private float totalPrice;
 private Member member;
 private Set products;
 //getters and setters ignored
public String toString(){//}
}

 Product.java:

view plaincopy to clipboardprint?
public class Product {  
    private int id;  
    private String name;  
    private float price;  
    private Set orders;  
    //  getters and setters ignored  
    public String toString(){  
        return "/nid: " + id + "/nname: " + name + "/nprice: " + price;  
    } 
public class Product {
 private int id;
 private String name;
 private float price;
 private Set orders;
 // getters and setters ignored
 public String toString(){
  return "/nid: " + id + "/nname: " + name + "/nprice: " + price;
 }

Order.hbm.xml:

view plaincopy to clipboardprint?
<hibernate-mapping> 
    <class name="model.Order" table="myorder" schema="dbo" catalog="hibernate"> 
        <id name="id" column="id"> 
            <generator class="native" /> 
        </id> 
        <property name="totalPrice" type="java.lang.Float"> 
            <column name="total_price" not-null="true" /> 
        </property> 
        <many-to-one name="member" class="model.Member" column="member_id" /> 
        <!-- 多对多的映射中, 中间表table是必须指定的 --> 
        <set name="products" table="dbo.order_line" cascade="save-update" fetch="join"> 
            <key column="order_id" /> 
            <many-to-many class="model.Product" column="product_id" /> 
        </set> 
    </class> 
</hibernate-mapping> 
<!--   
create table order(  
    id int primary key,  
    total_price float not null,  
    member_id int not null  
)   
 --> 
<hibernate-mapping>
 <class name="model.Order" table="myorder" schema="dbo" catalog="hibernate">
  <id name="id" column="id">
   <generator class="native" />
  </id>
  <property name="totalPrice" type="java.lang.Float">
   <column name="total_price" not-null="true" />
  </property>
  <many-to-one name="member" class="model.Member" column="member_id" />
  <!-- 多对多的映射中, 中间表table是必须指定的 -->
  <set name="products" table="dbo.order_line" cascade="save-update" fetch="join">
   <key column="order_id" />
   <many-to-many class="model.Product" column="product_id" />
  </set>
 </class>
</hibernate-mapping>
<!--
create table order(
 id int primary key,
 total_price float not null,
 member_id int not null
)
 -->

Product.hbm.xml:

view plaincopy to clipboardprint?
<hibernate-mapping> 
    <class name="model.Product" table="product" schema="dbo" catalog="hibernate"> 
        <id name="id" column="id"> 
            <generator class="native" /> 
        </id> 
        <property name="name" type="java.lang.String"> 
            <column name="name" length="50" not-null="true" /> 
        </property> 
        <property name="price" type="java.lang.Float"> 
            <column name="price" precision="2" scale="0"/> 
        </property> 
        <!-- 多对多的映射中, 中间表table是必须指定的 --> 
        <set name="orders" table="dbo.order_line" cascade="save-update" fetch="join" lazy="false"> 
            <key column="product_id" /> 
            <many-to-many class="model.Order" column="order_id" /> 
        </set> 
    </class> 
</hibernate-mapping> 
<!--   
create table product(  
    id int primary key,  
    name varchar(50) not null,  
    price float  
)   
 
create table etl.order_line(  
    order_id int,  
    product_id int  
)  
alter table order_line add constraint fk_orderline_order_id foreign key (order_id) references order(id)  
alter table order_line add constraint fk_orderline_product_id foreign key (product_id) references product(id)  
 --> 
<hibernate-mapping>
 <class name="model.Product" table="product" schema="dbo" catalog="hibernate">
  <id name="id" column="id">
   <generator class="native" />
  </id>
  <property name="name" type="java.lang.String">
   <column name="name" length="50" not-null="true" />
  </property>
  <property name="price" type="java.lang.Float">
   <column name="price" precision="2" scale="0"/>
  </property>
  <!-- 多对多的映射中, 中间表table是必须指定的 -->
  <set name="orders" table="dbo.order_line" cascade="save-update" fetch="join" lazy="false">
   <key column="product_id" />
   <many-to-many class="model.Order" column="order_id" />
  </set>
 </class>
</hibernate-mapping>
<!--
create table product(
 id int primary key,
 name varchar(50) not null,
 price float
)

create table etl.order_line(
    order_id int,
 product_id int
)
alter table order_line add constraint fk_orderline_order_id foreign key (order_id) references order(id)
alter table order_line add constraint fk_orderline_product_id foreign key (product_id) references product(id)
 -->

到现在, 从会员到订单, 从订单到产品都关联起来了,可以修改一下MemberHibernateDao.java的测试用例, 测试以下从一个Member对象一直找到Product.

view plaincopy to clipboardprint?
public static void main(String[] args){  
    MemberDao dao = new MemberHibernateDao();  
    Member m = dao.getById(2);  
    Set<Order> s = m.getOrders();  
    Iterator<Order> itr = s.iterator();  
    System.out.println("member " + m.getName() + " has " + s.size() + "orders.");  
    while(itr.hasNext()){  
        Order o = itr.next();  
        System.out.println("********************************");  
        System.out.println("Order ID: " + o.getId());  
          
        Set<Product> set = o.getProducts();  
        System.out.println("this order assosicate with " + set.size() + " procucts");  
        System.out.println("~ ~ ~ ~ ~ ~ ~");  
        for(Product p: set){  
            System.out.println(p);  
        }  
    }  

 

本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/sunxing007/archive/2009/08/24/4477471.aspx

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值