zoukankan      html  css  js  c++  java
  • 继承关系中的第三种方式:利用<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>的一种优势。

    如果没有一直坚持,也不会有质的飞跃,当生命有了限度,每个人的价值就会浮现。
  • 相关阅读:
    删除链表的倒数第N个节点
    SVN快速入门(TSVN)
    C# HttpWebRequest提交数据方式浅析
    简单的3个SQL视图搞定所有SqlServer数据库字典
    简单统计SQLSERVER用户数据表大小(包括记录总数和空间占用情况)
    详细讲解Android对自己的应用代码进行混淆加密防止反编译
    PHP之网络编程
    PHP之ThinkPHP模板标签操作
    PHP之ThinkPHP数据操作CURD
    关于数组的取极值和排序
  • 原文地址:https://www.cnblogs.com/shiguangshuo/p/4103412.html
Copyright © 2011-2022 走看看