继承映射
3种方式,继承关系映射到数据库的表中
1.继承关系层次一个表(Table per class hierarchy),这种方式使用一个表存储同一个继
承层次的所有类,使用额外的字段来表示所记录的是哪一个子类的数据。
2.每一个具体类一个表(Table per concrete class),这种方式让继承关系中的每个具体类
对应一个表
3.每个子类一个表(Table per subclass),这种方式让继承关系中的父类和每一个子类分别
对应一个表,父类与子类对应的表通过外键产生关联
继承关系层次一个表
public class Delivery implements java.io.Serializable{ private Integer id; private String recipient; private String phone; private String address; private String postcode; }
public class PostDelivery extends Delivery{ private String parcelNumber; }
public class ExpressDelivery extends Delivery{ private String expressCompany; private String expressNumber; }
并且生成getter和setter方法
在delivery.hbm.xml修改
在id 下面加入
<!--以delivery_type区分--> <discriminator column="delivery_type"></discriminator> <!--子类PostDelivery特有的属性--> <subclass name="com.rbh.examples.PostDelivery" discriminator-value="Post"> <property name="paracelNumber" type="java.lang.String"> <column name="PARACEL_NUMBER" length="30" /> </property> </subclass> <!--子类ExpressDelivery特有的属性--> <subclass name="com.rbh.examples.ExpressDelivery" discriminator- value="Express"> <property name="expressNumber" type="java.lang.String"> <column name="EXPRESS_NUMBER" length="30" /> </property> <property name="expressCompany" type="java.lang.String"> <column name="EXPRESS_COMPANY" length="30" /> </property> </subclass>
每个具体类一个表
public abstract class Delivery implements java.io.Serializable { private Integer id; private String recipient; private String phone; private String address; private String postcode; }
<class name="com.rbh.examples.Delivery" table="DELIVERY" schema="SCOTT" abstract="true"> 为抽象类 <union-subclass name="com.rbh.examples.PostDelivery" table="post_delivery"> <property name="parcelNumber" type="java.lang.String"> <column name="PARCEL_NUMBER" length="30" /> </property> </union-subclass> <union-subclass name="com.rbh.examples.ExpressDelivery" table="express_delivery"> <property name="expressNumber" type="java.lang.String"> <column name="EXPRESS_NUMBER" length="30" /> </property> <property name="expressCompany" type="java.lang.String"> <column name="EXPRESS_COMPANY" length="30" /> </property> </union-subclass>
每个子类一个表
public class Delivery implements java.io.Serializable{ private Integer id; private String recipient; private String phone; private String address; private String postcode; }
public class PostDelivery extends Delivery{ private String parcelNumber; }
public class ExpressDelivery extends Delivery{ private String expressCompany; private String expressNumber; }
<joined-subclass name="com.rbh.examples.PostDelivery" table="post_delivery"> <key column="id" foreign-key="id"></key> <property name="parcelNumber" type="java.lang.String"> <column name="PARCEL_NUMBER" length="30" /> </property> </joined-subclass> <joined-subclass name="com.rbh.examples.ExpressDelivery" table="express_delivery"> <key column="id" foreign-key="id"></key> <property name="expressNumber" type="java.lang.String"> <column name="EXPRESS_NUMBER" length="30" /> </property> <property name="expressCompany" type="java.lang.String"> <column name="EXPRESS_COMPANY" length="30" /> </property> </joined-subclass>