《四》每个具体类映射一张独立的表(union-subclass),即表与子类之间的独立一对一关系
所有的子类的表中的信息都是完整的,不需要对表进行关联了。
person.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">
<hibernate-mapping package="hibernate.extend" auto-import="false">
<class name="Person" table="person">
<id name="id">
<generator class="hilo"/>
</id>
<property name="name"/>
<property name="age"/>
<union-subclass name="Worker" table="worker">
<property name="work_year"/>
</union-subclass>
<union-subclass name="Farmer" table="farmer">
<property name="farm_name"/>
</union-subclass>
</class>
</hibernate-mapping>
产生的sql语句为:
Hibernate: insert into worker (name, age, work_year, id) values (?, ?, ?, ?)
Hibernate: insert into farmer (name, age, farm_name, id) values (?, ?, ?, ?)
Hibernate: select person0_.id as id8_0_, person0_.name as name8_0_, person0_.age as age8_0_, person0_.work_year as work1_9_0_, person0_.farm_name as farm1_10_0_, person0_.clazz_ as clazz_0_ from ( select null as work_year, id, age, name, null as farm_name, 0 as clazz_ from person union select work_year, id, age, name, null as farm_name, 1 as clazz_ from worker union select null as work_year, id, age, name, farm_name, 2 as clazz_ from farmer ) person0_ where person0_.id=?
执行后,数据库中表结构为:
person表:
+----+--------+------+
| id | name | age |
+----+--------+------+
| 1 | person | 22 |
+----+--------+------+
worker表:
+----+--------+------+----------------+
| id | name | age | work_year |
+----+--------+------+----------------+
| 2 | worker | 30 | 11 |
+----+--------+------+----------------+
farmer表:
+----+--------+------+--------------+
| id | name | age | farm_name|
+----+--------+------+--------------+
| 3 | farmer | 31 | little candy |
+----+--------+------+--------------+
注意:注意到我们把主键生成器改为使用高低键生成器了。因为native会根据使用的数据库自动选择相应的主键生成方式,由于我使用的是mysql数据库,对主键字段是自动增长的。也就是说在三个表中会有相同的主键id。这样在查询时就会出现问题。当你查一个id为1的person时,他会从三张表中都获得结果,这显然不行。所以这里改用高低键hilo生成方式,就不会出现这种问题。从上面的表中的数据也可以看出来。三个表的id是互不相同的。这种设计更符合实际需求。
补充:
同时,有时候父类是抽象类,这样就不需要在数据库中产生表了。这只需要加一个属性配置一下即可:
<?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">
<hibernate-mapping package="hibernate.extend" auto-import="false" abstract="true" >
<class name="Person" table="person">
<id name="id">
<generator class="hilo" />
</id>
<property name="name"/>
<property name="age"/>
<union-subclass name="Worker" table="worker">
<property name="work_year"/>
</union-subclass>
<union-subclass name="Farmer" table="farmer">
<property name="farm_name"/>
</union-subclass>
</class>
</hibernate-mapping>