为了持久化类之间的继承关系映射到数据库中,可以采用
为每个具体的子类对应一个关系表,没有父类数据表
数据表:
customer:
id int(4) <pk>
name varchar(20)
phone varchar(20)
oneoffOrders:一次性付款订单表
id int(4) <pk>
customer_id int(4)
orderno varchar(20)
moeny decimal(10,2)
rebate decimal(10,2) 折扣金额
dividedOrders:分期付款订单表
id int(4) <pk>
customer_id int(4)
moeny decimal(10,2)
rebate decimal(10,2)
months int(4)
持久化类:
public class Customer implements Serializable{
private Integer id;
private String name;
private String phone;
private Set orders=new HashSet();
pulic Customer(){
}
}
//父类
public abstrace class Orders{
private Integer id;
private String orderno;
private Double moeny;
private Customer customer;
public Orders(){
}
}
//子类 Oneofforders
public class Oneofforders extends Orders implements Serializable{
private Double rebate;
public Oneofforders(){
}
}
//子类 Dividedorders
public class Dividedorders extends Orders implements Serializable}
private Integer months;
public Dividedorders(){
}
}
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"/>
<property name="phone" column="phone" type="string"/>
<set name="orders" lazy="false" cascade="all" inverse="true">
<key column="customer_id"/>
<one-to-many class="com.lhb.vo.Orders"/>
</set>
</class>
</hibernate-mapping>
<hibernate-mapping package="com.lhb.vo">
<class name="Oneofforders" table="oneofforders">
<id name="id" column="id" type="integer">
<generator class="native"/>
</id>
<property name="orderno" column="orderno" type="string"/>
<property name="ordername" column="ordername" type="string"/>
<property name="rebate" column="rebate" type="double"/>
<many-to-one name="customer" column="customer_id" class="com.lhb.vo.Customer" lazy="false" not-null="true"/>
</class>
</hibernate-mapping>
<hibernate-mapping package="com.lhb.vo">
<class name="Dividedorders" table="dividedorders">
<id name="id" column="id" type="integer">
<generator class="native"/>
</id>
<property name="orderno" column="orderno" type="string"/>
<property name="money" column="money" type="double"/>
<proerty name="months" column="months" type="double"/>
<many-to-one name="customer" column="customer_id" class="com.lhb.vo.Customer" lazy="false" not-null="true"/>
</class>
</hibernate-mapping>
posted on 2008-05-25 21:32
长春语林科技 阅读(285)
评论(0) 编辑 收藏 所属分类:
hibernate