多对多关联在关系数据库中不能直接实现,还必须依赖一张
连接表保存这种关联关系
订单表:orders 商品表:items 连接表:selecteditems
orders:
id int(4) <pk>
orderno varchar(20)
moeny decimal(10,2)
items:
id int(4) <pk>
itemno varchar(20)
itemname varchar(60)
selecteditems:
orderid int(4) <pk,fk1>
itemid int(4) <pk,fk2>
2.pojo类
public class Orders implements Serilizable}
private Integer id;
private String orderno;
private Double moeny;
private Set items=new HashSet();
public Orders(){
}
}
public class Items implements Serilizable{
private Integer id;
private String itemno;
private String itemname;
private Set orders=new HashSet();
public Items(){
}
}
3.hbm.xml
<hibernate-mapping package="com.lhb.vo">
<class name="Orders" table="orders">
<id name="id" column="id" type="integer">
<generator class="native"/>
</id>
<property name="orderno" column="orderno" type="string"/>
<property name="moeny" column="moeny" type="double"/>
<set name="items" cascade="save-update" lazy="true"
<!--指定连接表的名字-->
table="selecteditems">
<key column="order_id"/>
<many-to-many class="com.lhb.vo.Items"
<!--指定参照items表的外键名字-->
column="item_id"/>
</set>
</class>
</hibernate-mapping>
<hibernate-mapping package="com.lhb.vo">
<class name="Items" table="orders">
<id name="id" column="id" type="integer">
<generator class="native"/>
</id>
<property name="itemno" column="itemno" type="string"/>
<property name="itemname" column="itemname" type="double"/>
<set name="items" cascade="save-update" lazy="true" inverse="true"
<!--指定连接表的名字-->
table="selecteditems">
<key column="items_id"/>
<many-to-many class="com.lhb.vo.Orders"
<!--指定参照items表的外键名字-->
column="orders_id"/>
</set>
</class>
</hibernate-mapping>
posted on 2008-05-25 17:44
长春语林科技 阅读(338)
评论(0) 编辑 收藏 所属分类:
hibernate