首页 > 代码库 > 继承关系中的第三种方式:利用<union-subclass>(补充)
继承关系中的第三种方式:利用<union-subclass>(补充)
继承关系中的第三种方式:利用<union-subclass>
代码:
映射文件(其他的代码和其他继承关系相同)
Person.hbm.xml
<?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="qau.edu.union">
<class name="Person" table="t_person">
<id name="id" column="t_id"> <generator class="hilo"/> </id>
<property name="name" column="t_name"/> <property name="date" column="t_date"/>
<!-- 配置继承关系 (利用union-subclass进行)-->
<union-subclass name="Teacher" table="TEACHER">
<property name="job"/>
</union-subclass>
</class>
</hibernate-mapping>
|
执行结果:
进行Save操作的结果:
log4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environment). log4j:WARN Please initialize the log4j system properly. log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info. Hibernate: insert into t_person (t_name, t_date, t_id) values (?, ?, ?) Hibernate: insert into TEACHER (t_name, t_date, job, t_id) values (?, ?, ?, ?)
|
只有两条记录,说明还是比较方便的。
获取对象的操作的执行结果:
log4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environment). log4j:WARN Please initialize the log4j system properly. log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info. Hibernate: select person0_.t_id as t1_0_0_, person0_.t_name as t2_0_0_, person0_.t_date as t3_0_0_, person0_.job as job1_0_, person0_.clazz_ as clazz_0_ from ( select t_id, t_name, t_date, null as job, 0 as clazz_ from t_person union select t_id, t_name, t_date, job, 1 as clazz_ from TEACHER ) person0_ where person0_.t_id=? 名字是:AAA Hibernate: select teacher0_.t_id as t1_0_0_, teacher0_.t_name as t2_0_0_, teacher0_.t_date as t3_0_0_, teacher0_.job as job1_0_ from TEACHER teacher0_ where teacher0_.t_id=? 工作是:Teacher
|
通过这样的执行结果可以看出:在执行子类的查询的时候只是访问的是子类的表,但是进行父类的查询的时候却是进行的两张表的查询。这是比较麻烦的事情。
但是这是这种方式<union-subclass>的一种优势。
继承关系中的第三种方式:利用<union-subclass>(补充)