多對多關(guān)聯(lián)在關(guān)系數(shù)據(jù)庫中不能直接實(shí)現(xiàn),還必須依賴一張
連接表保存這種關(guān)聯(lián)關(guān)系
訂單表: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;
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>
posted on 2008-05-25 17:38
長春語林科技 閱讀(681)
評論(0) 編輯 收藏 所屬分類:
hibernate