`

hibernate继承关系映射

阅读更多
hbn 的继承映射关系有这几种:
   1. 继承关系类共同使用一个表;
   2. 每个子类一个表(mapping全);
   3. 每个具体内一个表(mapping只有父类)。


继承关系描述:
  Animals父类 -- Bird 、Reptiles;
  Animals.java
package TableBean;

/** 动物父类
* @hibernate.class
* table="animals"
*/
public class Animals {

    private Integer id ;
    private Integer size ;
    private Integer speed;
    /**
     * @hibernate.id
     * generator-class="increment"
     */
    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    /** 动物体积大小
     * @hibernate.property
     */
    public Integer getSize() {
        return size;
    }

    public void setSize(Integer size) {
        this.size = size;
    }
    /** 动物的移动速度
     * @hibernate.property
     */
    public Integer getSpeed() {
        return speed;
    }

    public void setSpeed(Integer speed) {
        this.speed = speed;
    }
   
}

Bird.java
package TableBean;

/** 在xdoeclt中声明继承表关系
* @hibernate.joined-subclass
* @hibernate.joined-subclass-key
*  column="id"
*
*/
public class Bird extends Animals {
    private String wings ;
    private Integer leg ;
    /** 鸟类翅膀描述
     * @hibernate.property
     * @return
     */
    public String getWings() {
        return wings;
    }

    public void setWings(String wings) {
        this.wings = wings;
    }

    /**鸟腿描述
     * @hibernate.property
     * @return
     */
    public Integer getLeg() {
        return leg;
    }

    public void setLeg(Integer leg) {
        this.leg = leg;
    }
   
}

Reptiles.java
package TableBean;

/** 在xdoeclt中声明继承表关系
* @hibernate.joined-subclass
* @hibernate.joined-subclass-key
*  column="id"
*/
public class Reptiles extends Animals {
    private Integer leg ;
    /** 爬行动物腿个数
     * @hibernate.property
     * @return
     */
    public Integer getLeg() {
       return leg;
    }

    public void setLeg(Integer leg) {
        this.leg = leg;
    }
   
}


ant_xdoeclt.xml文件
<?xml version="1.0" encoding="UTF-8"?>

<!-- DO NOT EDIT -->
<!-- This file is automatically regenerated for each new XDoclet generation. -->
<!-- Any changes you make to this file will be overwritten. If you want -->
<!-- to edit this file, make a copy and rename it for your own use. -->

<project name="Hbmzj XDoclet Generation" default="_xdoclet_generation_"><property file="xdoclet-build.properties"/>
<property name="xdoclet.basedir" value="/C:/Program Files/MyEclipse/eclipse/plugins/com.genuitec.jboss.ide.eclipse.xdoclet.core_4.0.0/"/>
<path id="xdoclet.classpath"><pathelement location="E:/hibernate/Hbmzj/bin"/>
<pathelement location="C:/Program Files/Java/jdk1.5.0_02/jre/lib/rt.jar"/>
<pathelement location="C:/Program Files/Java/jdk1.5.0_02/jre/lib/jsse.jar"/>
<pathelement location="C:/Program Files/Java/jdk1.5.0_02/jre/lib/jce.jar"/>
<pathelement location="C:/Program Files/Java/jdk1.5.0_02/jre/lib/charsets.jar"/>
<pathelement location="C:/Program Files/Java/jdk1.5.0_02/jre/lib/ext/sunjce_provider.jar"/>
<pathelement location="C:/Program Files/Java/jdk1.5.0_02/jre/lib/ext/sunpkcs11.jar"/>
<pathelement location="C:/Program Files/Java/jdk1.5.0_02/jre/lib/ext/dnsns.jar"/>
<pathelement location="C:/Program Files/Java/jdk1.5.0_02/jre/lib/ext/localedata.jar"/>
<pathelement location="E:/hibernate/Hbmzj/lib/antlr-2.7.5H3.jar"/>
<pathelement location="E:/hibernate/Hbmzj/lib/asm-attrs.jar"/>
<pathelement location="E:/hibernate/Hbmzj/lib/asm.jar"/>
<pathelement location="E:/hibernate/Hbmzj/lib/c3p0-0.8.5.2.jar"/>
<pathelement location="E:/hibernate/Hbmzj/lib/cglib-2.1.jar"/>
<pathelement location="E:/hibernate/Hbmzj/lib/cleanimports.jar"/>
<pathelement location="E:/hibernate/Hbmzj/lib/commons-collections-2.1.1.jar"/>
<pathelement location="E:/hibernate/Hbmzj/lib/commons-logging-1.0.4.jar"/>
<pathelement location="E:/hibernate/Hbmzj/lib/concurrent-1.3.2.jar"/>
<pathelement location="E:/hibernate/Hbmzj/lib/connector.jar"/>
<pathelement location="E:/hibernate/Hbmzj/lib/dom4j-1.6.jar"/>
<pathelement location="E:/hibernate/Hbmzj/lib/ehcache-1.1.jar"/>
<pathelement location="E:/hibernate/Hbmzj/lib/hibernate3.jar"/>
<pathelement location="E:/hibernate/Hbmzj/lib/jaas.jar"/>
<pathelement location="E:/hibernate/Hbmzj/lib/jacc-1_0-fr.jar"/>
<pathelement location="E:/hibernate/Hbmzj/lib/jaxen-1.1-beta-4.jar"/>
<pathelement location="E:/hibernate/Hbmzj/lib/jdbc2_0-stdext.jar"/>
<pathelement location="E:/hibernate/Hbmzj/lib/jgroups-2.2.7.jar"/>
<pathelement location="E:/hibernate/Hbmzj/lib/jta.jar"/>
<pathelement location="E:/hibernate/Hbmzj/lib/log4j-1.2.9.jar"/>
<pathelement location="E:/hibernate/Hbmzj/lib/oscache-2.1.jar"/>
<pathelement location="E:/hibernate/Hbmzj/lib/proxool-0.8.3.jar"/>
<pathelement location="E:/hibernate/Hbmzj/lib/swarmcache-1.0rc2.jar"/>
<pathelement location="E:/hibernate/Hbmzj/lib/xerces-2.6.2.jar"/>
<pathelement location="E:/hibernate/Hbmzj/lib/xml-apis.jar"/>
<pathelement location="D:/eclipse2/eclipse/plugins/org.junit_3.8.1/junit.jar"/>
<pathelement location="E:/hibernate/Hbmzj/lib/MYSQL.JAR"/>

<fileset dir="${xdoclet.basedir}">
<include name="*.jar"/>
<exclude name="*xjavadoc*.jar"/>
<include name="jdk5/*.jar"/>
</fileset>
</path>
<target name="_xdoclet_generation_" depends="N65540"/>
<target name="N65540" description="Standard Hibernate">
<taskdef name="hibernatedoclet"
         classname="xdoclet.modules.hibernate.HibernateDocletTask"
         classpathref="xdoclet.classpath"/>

<hibernatedoclet excludedTags="@version,@author,@todo,@see"
               destDir="src"
addedTags="@xdoclet-generated at ${TODAY},@copyright The XDoclet Team,@author XDoclet,@version ${version}" >

<fileset dir="src"  includes="**/*.java" >
</fileset>

<hibernate>
</hibernate>
</hibernatedoclet>
</target>
</project>

ant运行 后得到 Animals.hbm.xml 文件
使用 hbn工具 hbn2ddl 后
mysql> desc  Animals;
+-------+---------+------+-----+---------+-------+
| Field | Type    | Null | Key | Default | Extra |
+-------+---------+------+-----+---------+-------+
| id    | int(11) |      | PRI | 0       |       |
| size  | int(11) | YES  |     | NULL    |       |
| speed | int(11) | YES  |     | NULL    |       |
+-------+---------+------+-----+---------+-------+
3 rows in set (0.00 sec)

mysql> desc reptiles;
+-------+---------+------+-----+---------+-------+
| Field | Type    | Null | Key | Default | Extra |
+-------+---------+------+-----+---------+-------+
| id    | int(11) |      | PRI | 0       |       |
| leg   | int(11) | YES  |     | NULL    |       |
+-------+---------+------+-----+---------+-------+
2 rows in set (0.00 sec)

mysql> desc bird;
+-------+--------------+------+-----+---------+-------+
| Field | Type         | Null | Key | Default | Extra |
+-------+--------------+------+-----+---------+-------+
| id    | int(11)      |      | PRI | 0       |       |
| wings | varchar(255) | YES  |     | NULL    |       |
| leg   | int(11)      | YES  |     | NULL    |       |
+-------+--------------+------+-----+---------+-------+
3 rows in set (0.00 sec)


添加测试数据:
mysql> select * from bird;
+----+-------+------+
| id | wings | leg  |
+----+-------+------+
|  1 | 2G    |    2 |
+----+-------+------+
1 row in set (0.00 sec)

mysql> select * from Animals;
+----+------+-------+
| id | size | speed |
+----+------+-------+
|  1 |    1 |     9 |
|  2 |    6 |     2 |
+----+------+-------+
2 rows in set (0.00 sec)

mysql> select * from Reptiles;
+----+------+
| id | leg  |
+----+------+
|  2 |    4 |
+----+------+
1 row in set (0.00 sec)



测试
public class TestJc extends TestCase {
    public void testJC() throws Exception {
        Session session= HbnFactory.currentSession();
        Transaction tr = session.beginTransaction();
        //1
        Animals aa = (Animals)session.load(Animals.class,1);
        System.out.println(aa);
        //2
        Bird bb =  (Bird) session.load(Bird.class,1);
        System.out.println(bb);
        //3
        Object obj = session.createQuery(" from Animals ").list();
        System.out.println(obj);
        
        tr.commit();
        HbnFactory.closeSession();
    }
}
结果:
//1
Hibernate: select animals0_.id as id0_, animals0_.size as size0_0_, animals0_.speed as speed0_0_, animals0_1_.wings as wings1_0_, animals0_1_.leg as leg1_0_, animals0_2_.leg as leg2_0_, case when animals0_1_.id is not null then 1 when animals0_2_.id is not null then 2 when animals0_.id is not null then 0 end as clazz_0_ from animals animals0_ left outer join Bird animals0_1_ on animals0_.id=animals0_1_.id left outer join Reptiles animals0_2_ on animals0_.id=animals0_2_.id where animals0_.id=?
TableBean.Bird@5e176f
//2
TableBean.Bird@5e176f
//3
Hibernate: select animals0_.id as id, animals0_.size as size0_, animals0_.speed as speed0_, animals0_1_.wings as wings1_, animals0_1_.leg as leg1_, animals0_2_.leg as leg2_, case when animals0_1_.id is not null then 1 when animals0_2_.id is not null then 2 when animals0_.id is not null then 0 end as clazz_ from animals animals0_ left outer join Bird animals0_1_ on animals0_.id=animals0_1_.id left outer join Reptiles animals0_2_ on animals0_.id=animals0_2_.id
[TableBean.Bird@5e176f, TableBean.Reptiles@2c1e6b, TableBean.Reptiles@811c88]

 

分享到:
评论

相关推荐

    Hibernate继承关系映射.pdf

    标题:“Hibernate继承关系映射.pdf” 描述:“简明扼要地介绍了Hibernate中继承关系的映射方式,深入探讨了三种不同的映射策略及其优缺点,同时解析了多态查询的概念。” 知识点: ### Hibernate继承关系映射...

    Hibernate继承映射代码

    本主题将深入探讨"Hibernate继承映射+C3P0代码"的相关知识点。 首先,让我们理解Hibernate的继承映射。在Java中,我们可以创建一个基类,然后派生出多个子类,这种设计模式在数据库中也可以被映射出来。Hibernate...

    用Hibernate映射继承关系

    映射文件应定义该类的属性与数据库表中的列之间的映射关系。例如,对于`HourlyEmployee`类,映射文件可能如下所示: ```xml ``` 类似的,`SalariedEmployee`类也有其对应的映射文件。 ##### 14.1.2 ...

    hibernate继承映射.rar

    Hibernate继承映射是将Java中的继承关系映射到数据库的关系模型中。在Java中,一个基类可以有多个子类,而在数据库中,这些子类可以共享一张表或者各自拥有独立的表,这取决于我们选择的继承策略。Hibernate提供了四...

    hibernate的继承映射关系

    ### Hibernate的继承多态映射关系详解 在软件开发领域,特别是使用面向对象编程语言如Java进行开发时,我们经常需要处理复杂的类继承关系。在ORM(Object-Relational Mapping)框架如Hibernate中,如何优雅地将这些...

    Hibernate继承映射的第一种策略:每棵类继承树对应一张表

    Hibernate继承映射是将Java类的继承关系映射到数据库表的一种策略,使得对象模型的复杂性能够平滑地转化为关系数据库模型。本篇将详细介绍Hibernate继承映射的第一种策略——每棵类继承树对应一张表,即单一表继承...

    hibernate映射继承关系(每个类都对应一张表)

    总结起来,"每个类都对应一张表"的继承映射策略在Hibernate中是一种直接且易于理解的方法,适合那些每个类都有独特属性的情况。然而,它可能不适合所有场景,特别是当子类众多或者需要减少数据冗余时。在实际应用中...

    Hibernate继承映射

    Hibernate的继承映射 --每个继承层次一张表 -- 每个具体类一张表 -- 每个类一张表

    Hibernate继承映射-概述

    《Hibernate继承映射详解》 在Java开发中,对象关系映射(ORM)框架如Hibernate大大简化了数据库操作。Hibernate不仅提供了对基本数据类型的映射,还支持复杂的数据结构,如继承关系的映射。本篇文章将深入探讨...

    Hibernate继承映射(annotation)

    **标题:“Hibernate继承映射(Annotation)详解”** 在Java持久化框架Hibernate中,继承映射是一种关键特性,它允许开发者将对象模型的继承结构映射到数据库的表结构。在传统的面向对象编程中,继承是实现代码复用和...

    hibernate继承映射教学

    针对hibernate的继承映射部分做了几个测试的例子,更加直观的阐述了继承映射的几种方式以及对应的表结构方式,文件被分割成3部分 http://download.csdn.net/source/259075 http://download.csdn.net/source/259072 ...

    hibernate的继承映射教学

    针对hibernate的继承映射部分做了几个测试的例子,更加直观的阐述了继承映射的几种方式以及对应的表结构方式,文件被分割成3部分 http://download.csdn.net/source/259075 http://download.csdn.net/source/259072 ...

    Hibernate继承映射二:每个子类一张表

    总结来说,“每个子类一张表”的继承映射策略是Hibernate提供的一种处理继承关系的方法,它将类的继承结构映射到数据库的多个表中。这种策略适合于子类具有大量特有属性的情况,但需要权衡可能带来的数据库设计复杂...

Global site tag (gtag.js) - Google Analytics