• 继承关系中的第三种方式:利用<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>的一种优势。

    如果没有一直坚持,也不会有质的飞跃,当生命有了限度,每个人的价值就会浮现。
  • 相关阅读:
    manage.py migrate 报错
    pip安装django出错 Could not install packages due to an EnvironmentError: [Errno 13]
    利用beautifulsoup下载网页html代码中的css, js, img文件并保存
    itchat库微信自动回复祝福语
    关于阿里云Mysql分页查询不走索引的问题
    如何成为PHP程序员?
    Linux常用命令之scp
    PHP版本的区别与用法详解
    Linux常用命令之ftp
    Linux常用命令之权限管理
  • 原文地址:https://www.cnblogs.com/shiguangshuo/p/4103412.html
Copyright © 2020-2023  润新知