posts - 165, comments - 198, trackbacks - 0, articles - 1
  BlogJava :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理

hbn 继承关系映射_1

Posted on 2007-12-26 11:41 G_G 阅读(1087) 评论(0)  编辑  收藏 所属分类: hibernate
尝试着继承关系和xdoclet使用后,对数据库面向对象的使用更近一步。
这次例子从开始到结束,都没有接触面向结构的数据库设计;
并完全在代码中以面向对象的形式来设计的。


hbn 的继承映射关系有这几种;
  1. 继承关系类共同使用一个表;
  2. 每个子类一个表(mapping全);
  3. 每个具体内一个表(mapping只有父类)。
先说说3把,感觉这点最好用-》》
继承关系描述:
  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]




只有注册用户登录后才能发表评论。


网站导航: