继承映射

主要介绍两种:

 

 

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

例如: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"?>
<!DOCTYPE hibernate-mapping PUBLIC
 "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
 "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.pk.po" >
 
 <class name="Person" table="t_person_m_b_s">
  <id name="id">
   <generator class="native"></generator>
  </id>
  <discriminator column="type"></discriminator>
  <property name="name" type="string"></property>
  <property name="age" type="integer"></property>
  
  <subclass name="Manager" discriminator-value="manager" >
   <property name="manager" type="string" ></property>
  </subclass>
  
  <subclass name="Boss" discriminator-value="boss" >
   <property name="boss" type="string"></property>
  </subclass>
  
  <subclass name="Saleman" discriminator-value="saleman">
   <property name="sale" type="string"></property>
  </subclass>

 
 </class>

</hibernate-mapping>

 

 

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

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

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
 "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
 "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.pk.po" >
 
 <class name="Person" table="t_person" lazy="false" >
  <id name="id">
   <generator class="native"></generator>
  </id>

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

  <joined-subclass name="Manager" table="t_manager">
   <key column="manager_id"></key>
   <property name="manager" type="string" ></property>
  </joined-subclass>
  
  <joined-subclass name="Boss" table="t_boss">
   <key column="boss_id"></key>
   <property name="boss" type="string" ></property>
  </joined-subclass>
  
  <joined-subclass name="Saleman" table="t_saleman">
   <key column="saleman_id"></key>
   <property name="sale" type="string"></property>
  </joined-subclass>


 
 </class>

</hibernate-mapping>

你可能感兴趣的:(Hibernate,.net,xml)