hibernate学习之第十一篇(2)

《二》每个子类映射到一张表(joined-subclass)
配置文件修改为:

<?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="native"/>
        </id>

        <property name="name"/>
        <property name="age"/>

        <joined-subclass name="Worker" table="worker" >
            <key column="person_id"></key> //指定外键关联的列
            <property name="work_year"/>
        </joined-subclass>
        <joined-subclass name="Farmer" table="farmer" >
            <key column="person_id"/>    //指定外键关联的列
            <property name="farm_name"/>
        </joined-subclass>
    </class>
</hibernate-mapping>

上面的配置中,我们并没有单独为worker和farmer编写映射文件,而是通过joined-subclass节点在父类映射文件中对子类进行了配置。


仍然执行上面的测试代码,查看输出的sql语句为:

写道
Hibernate: insert into person (name, age) values (?, ?)
Hibernate: insert into person (name, age) values (?, ?)
Hibernate: insert into worker (work_year, person_id) values (?, ?)
Hibernate: insert into person (name, age) values (?, ?)
Hibernate: insert into farmer (farm_name, person_id) values (?, ?)
 

这时数据库中就有三张表了,分别为:person表,worker表,farmer表。结构如下:
person:                      
+----+--------+------+     
| id | name   | age  |   
+----+--------+------+
|  1 | person |   22 |
|  2 | worker |   30 |
|  3 | farmer |   31 |
+----+--------+------+
 worker:
+-----------+----------------+
| person_id | work_year |
+-----------+----------------+
|         2    |        11         |
+-----------+----------------+

farmer:
+-----------+-----------------+
| person_id | farm_name |
+-----------+-----------------+
|         3     | little candy    |
+-----------+-----------------+

查询:

static void query(int id) {
        Session s = HibernateUtil.getSession();
        Transaction tx = s.beginTransaction();
        Worker worker = (Worker) s.get(Worker.class, id);
        tx.commit();
        System.out.println("year: "+worker.getWork_year());
    }
 

调用query(2);
打印输出的sql语句为:

Hibernate: select worker0_.person_id as id8_0_, worker0_1_.name as name8_0_, worker0_1_.age as age8_0_, worker0_.work_year as work2_9_0_ from worker worker0_ inner join person worker0_1_ on worker0_.person_id=worker0_1_.id where worker0_.person_id=?
year: 11
 



如果把着色部分修改为:Worker worker = (Worker) s.get(Person.class , id);
打印输出的sql语句为:

Hibernate: select person0_.id as id8_0_, person0_.name as name8_0_, person0_.age as age8_0_, person0_1_.work_year as work2_9_0_, person0_2_.farm_name as farm2_10_0_, case when person0_1_.person_id is not null then 1 when person0_2_.person_id is not null then 2 when person0_.id is not null then 0 end as clazz_0_ from person person0_ left outer join worker person0_1_ on person0_.id=person0_1_.person_id left outer join farmer person0_2_ on person0_.id=person0_2_.person_id where person0_.id=?
year: 11
 


 由此可见:hibernate支持多态查询。在查询语句中,指定父类类型,hibernate会关联三张表到其中去查,而指定了具体的类型,则只会查找相关的表。如果子类有很多,那么最好明确指定查的子类,否则,对多表关联查询,会带来性能问题。

你可能感兴趣的:(数据结构,sql,Hibernate,xml,.net)