1.數據表:
orders:訂單表 customer:客戶表
orders:
id int(4) <pk>
orderno varchar(20)
moeny decimal(10,2)
customer_id int(4) <fk>
customer:
id int(4) <pk>
name varchar(20)
phone varchar(20)
2.pojo類
public class Customer implements Serilizable{
private Integer id;
private String name;
private String phone;
private Set orders=new hashSet();
public Customer(){
}
}
public class Orders implements Serilizable{
private Integer id;
private String orderno;
private Double moeny;
private Customer customer;
public Orders(){
}
}
3.hbm.xml
Orders.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="moeny" column="moeny" type="double"/>
<property name="orderno" column="orderno" type="string"/>
<many-to-one column="customer_id" class="com.lhb.Customer" lazy="false" not-null="true"/>
</class>
</hibernate-mapping>
Customer.hbm.xml
<hibernate-mapping package="com.lhb.vo">
<class name="Customer" table="customer">
<id name="id" column="id" type="integer">
<generator class="native"/>
</id>
<property name="name" column="name" type="string"/>
<proerty name="phone" column="phone" type="string"/>
<!--all:表示所有操作均在關聯層級上時行連鎖操作
save-update:表示只有save與update操作進行連鎖操作
delete:表示只有delete操作進行連鎖操作
延遲加載lazy:就是在用的時候再進行加載
inverse:表示關聯關系維護工作由誰負責,默認為false,表示由主控方負責維護關聯關系,如果設置為tue,表示由被控方來維護
-->
<set name="orders" cascade="all" lazy="false" inverse="true" >
<!--指出充當外鍵的字段名-->
< key column="customer_id"/>
<!--指出關聯類的名字,表名集合中存放的是該類的對象-->
<one-to-many class="com.lhb.Orders"/>
</set>
</class>
</hibernate-mapping>
posted on 2008-05-25 17:10
長春語林科技 閱讀(582)
評論(0) 編輯 收藏 所屬分類:
hibernate