继承关系映射方式:每个类都映射为一张表
实体模型:
Company:Integer id;String name;Set employees=newHashSet();
Employee:Integer id;String name;Company company;父类只含有公共属性,以及关联类属性
HourlyEmployees extendsEmployee:Integer employeeId;Double rate;子类只拥有自己的属性
SalariedEmployees extendsEmployee:Integer employeeId;Double salary;
关系模型:
company表:
hourlyEmployees表:EMPLOYEE_ID是主键又是外键
salariedEmployees表:EMPLOYEE_ID是主键又是外键
employee表:
映射文件:
Company.hbm.xml:
<hibernate-mapping>
<class name="bean.Company"table="company" catalog="addressbooksample">
<id name="id"type="java.lang.Integer">
<column name="ID"/>
<generator class="native"/>
</id>
<property name="name"type="java.lang.String">
<column name="name"length="20" />
</property>
<set name="employees"inverse="true" lazy="false">
<key column="COMPANY_ID"/>
<one-to-many class="bean.Employee"/>
</set>
</class>
</hibernate-mapping>
Employee.hbm.xml:
<hibernate-mapping>
<class name="bean.Employee"table="employee" catalog="addressbooksample">
<id name="id" type="java.lang.Integer">
<column name="ID"/>
<generator class="native"/>
</id>
<property name="name"type="java.lang.String">
<column name="name"length="20" />
</property>
<many-to-one name="company"column="COMPANY_ID" class="bean.Company"/>
<joined-subclass name="bean.HourlyEmployees"table="hourly_employees">
<key column="EMPLOYEE_ID"/>
<property name="rate"/>
</joined-subclass>
<joined-subclass name="bean.SalariedEmployees" table="salaried_employees">
<key column="EMPLOYEE_ID"/>
<property name="salary"/>
</joined-subclass>
</class>
</hibernate-mapping>
注:<joined-subclass>用于映射子类
——————————总结—————————————————
缺点:表的数目比较多,并且表之间有外键参照关系;学要进行表的内连接或左外链接
优点:某个类的属性发生变化,只需要修改和这个类对应的表;支持关联查询