继承映射

主要介绍两种:

第一种:所有的类在同一个表中

例如:Person,Boss,Manager,Saleman

Boss,Manager,Saleman继承Person

Person的属性:

int id;String name; boolean sex;

Boss:

String boss;

Manager:

String manager;

Saleman:

String sale;

首先,要写上4个类, Person,Boss,Manager,Saleman;

其次,就是要注意映射文件的写法

<?xml version="1.0"?>

<!DOCTYPEhibernate-mappingPUBLIC

"-//Hibernate/HibernateMappingDTD3.0//EN"

"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mappingpackage="com.pk.po">

<classname="Person"table="t_person_m_b_s">

<idname="id">

<generatorclass="native"></generator>

</id>

<discriminatorcolumn="type"></discriminator>

<propertyname="name"type="string"></property>

<propertyname="age"type="integer"></property>

<subclassname="Manager"discriminator-value="manager">

<propertyname="manager"type="string"></property>

</subclass>

<subclassname="Boss"discriminator-value="boss">

<propertyname="boss"type="string"></property>

</subclass>

<subclassname="Saleman"discriminator-value="saleman">

<propertyname="sale"type="string"></property>

</subclass>

 </class>

</hibernate-mapping>

第二种:每个类在不同的表中

与第一种的区别就是配置文件的区别

<?xml version="1.0"?>

<!DOCTYPEhibernate-mappingPUBLIC

"-//Hibernate/HibernateMappingDTD3.0//EN"

"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mappingpackage="com.pk.po">

<classname="Person"table="t_person"lazy="false">

<idname="id">

<generatorclass="native"></generator>

  </id>

  <property name="name" type="string"></property>  <property name="age" type="integer"></property>

  <joined-subclass name="Manager" table="t_manager">

<keycolumn="manager_id"></key>

<propertyname="manager"type="string"></property>

</joined-subclass>

<joined-subclassname="Boss"table="t_boss">

<keycolumn="boss_id"></key>

<propertyname="boss"type="string"></property>

</joined-subclass>

<joined-subclassname="Saleman"table="t_saleman">

<keycolumn="saleman_id"></key>

<propertyname="sale"type="string"></property>

  </joined-subclass>

 </class>

</hibernate-mapping>

相关推荐