Hibernate继承映射方式之每个子类一张表

来源:互联网 发布:威纶通触摸屏编程 编辑:程序博客网 时间:2024/04/29 11:35

首先解释下“每个子类一张表”的含义:每个子类使用一张表只存储它特有的属性,然后与父类所对应的表以一对一主键关联的方式关联起来。

现在有四个类Company2、Employee2、SalaryEmployee2、HourlyEmployee2,其中Employee2是SalaryEmployee2、HourlyEmployee2的父类

下面首先给出这四个类所对应的数据库表,此四个类的POJO类同上一篇的大同小异,这里就不给出了。

-- ------------------------------ Table structure for `company2`-- ----------------------------DROP TABLE IF EXISTS `company2`;CREATE TABLE `company2` (  `id` int(11) NOT NULL auto_increment,  `name` varchar(32) NOT NULL,  PRIMARY KEY  (`id`)) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=utf8;-- ------------------------------ Table structure for `employee1`-- ----------------------------DROP TABLE IF EXISTS `employee1`;CREATE TABLE `employee1` (  `id` int(11) NOT NULL,  `name` varchar(128) NOT NULL,  `company_id` int(11) NOT NULL,  PRIMARY KEY  (`id`),  KEY `FK14AC0F237E367C52` (`company_id`),  CONSTRAINT `FK14AC0F237E367C52` FOREIGN KEY (`company_id`) REFERENCES `company` (`id`)) ENGINE=InnoDB DEFAULT CHARSET=utf8;-- ------------------------------ Table structure for `hourly_employee2`-- ----------------------------DROP TABLE IF EXISTS `hourly_employee2`;CREATE TABLE `hourly_employee2` (  `employee_id` int(11) NOT NULL auto_increment,  `rate` double(10,5) NOT NULL,  PRIMARY KEY  (`employee_id`),  CONSTRAINT `FK1_EM` FOREIGN KEY (`employee_id`) REFERENCES `employee2` (`id`) ON DELETE CASCADE ON UPDATE CASCADE) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;-- ------------------------------ Table structure for `salary_employee2`-- ----------------------------DROP TABLE IF EXISTS `salary_employee2`;CREATE TABLE `salary_employee2` (  `employee_id` int(11) NOT NULL auto_increment,  `salary` double(10,5) NOT NULL,  PRIMARY KEY  (`employee_id`),  CONSTRAINT `FK2_EM2` FOREIGN KEY (`employee_id`) REFERENCES `employee2` (`id`) ON DELETE CASCADE ON UPDATE CASCADE) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;
表hourly_employee2和表salary_employee2都是通过外键employee_id(参照表employee2的字段id)和表employee2来关联(一一对应),employee2存储hourly和salary的公共属性,而hourly_employee2和salary_employee2存储自己特有的属性并且通过外键和employee2关联。一条记录要获得其独有的信息,要通过employee2记录的主键到其对应的子表中查找主键值一样的记录然后取出它独有的信息。

下面给出配置文件

1、Company2.hbm.xml

<?xml version="1.0" encoding="utf-8"?><!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN""http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"><!--     Mapping file autogenerated by MyEclipse Persistence Tools--><hibernate-mapping>    <class name="com.xkey.hibernate.bean2.Company2" table="company2" catalog="hibernate">        <id name="id" type="java.lang.Integer">            <column name="id" />            <generator class="identity" />        </id>        <property name="name" type="java.lang.String">            <column name="name" length="32" not-null="true" />        </property>        <set name="employee2s" inverse="true" cascade="all" lazy="true">            <key>                <column name="company_id" not-null="true" />            </key>            <one-to-many class="com.xkey.hibernate.bean2.Employee2" />        </set>    </class></hibernate-mapping>
2、Employee2.hbm.xml

<?xml version="1.0" encoding="utf-8"?><!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN""http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"><!--     Mapping file autogenerated by MyEclipse Persistence Tools--><hibernate-mapping>    <class name="com.xkey.hibernate.bean2.Employee2" table="employee2" catalog="hibernate">        <id name="id" type="java.lang.Integer">            <column name="id" />            <generator class="identity" />        </id>        <many-to-one name="company2" class="com.xkey.hibernate.bean2.Company2" fetch="select">            <column name="company_id" not-null="true" />        </many-to-one>        <property name="name" type="java.lang.String">            <column name="name" length="32" not-null="true" />        </property>        <joined-subclass name = "com.xkey.hibernate.bean2.SalaryEmployee2" table="salary_employee2">        <key column = "employee_id"></key>        <property name="salary" column="salary" type="java.lang.Double">        </property>        </joined-subclass>                <joined-subclass name = "com.xkey.hibernate.bean2.HourlyEmployee2" table="hourly_employee2">        <key column = "employee_id"></key>        <property name="rate" column="rate" type="java.lang.Double">        </property>        </joined-subclass>    </class></hibernate-mapping>

<joined-subclass>标签需要包含一个key标签,这个标签指定了子类和父类之间是通过哪个字段来关联的。

该方式的继承映射是可以实现多态查询的,如果想查询子类(HourlyEmployee2)的数据,那么如下例:

public HourlyEmployee2 findByHourlyId(java.lang.Integer id) {log.debug("getting Employee2 instance with id: " + id);try {HourlyEmployee2 instance = (HourlyEmployee2) getSession().get("com.xkey.hibernate.bean2.HourlyEmployee2", id);return instance;} catch (RuntimeException re) {log.error("get failed", re);throw re;}}
该段代码是放在Employee2DAO.java中的。