- 浏览: 914776 次
- 性别:
- 来自: 北京
文章分类
- 全部博客 (537)
- Java SE (114)
- Struts (18)
- Hibernate (25)
- Spring (3)
- Page_Tech (41)
- Others (87)
- Database (29)
- Server (24)
- OpenSource_Tools (15)
- IDE_Tool (22)
- Algorithm (28)
- Interview (22)
- Test (28)
- Hardware (1)
- Mainframe (25)
- Web application (4)
- Linux (3)
- PHP (17)
- Android (1)
- Perl (6)
- ubuntu (1)
- Java EE (9)
- Web Analysis (5)
- Node.js (2)
- javascript (2)
最新评论
-
一键注册:
request.getRequestURL()和request.getRequestURI() -
SuperCustomer:
...
SED的暂存空间和模式空间 -
juyo_ch:
讲得挺好理解的,学习了
java 死锁及解决 -
chinaalex:
最后一题答案正确,但是分析有误.按照如下过程,上一行为瓶,下一 ...
zz智力题 -
liaowuxukong:
多谢博主啦,弱弱的了解了一点。
C++/Java 实现多态的方法(C++)
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() {
}
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 n+1问题
2010-10-21 11:05 931Hibernate中常会用到set,bag等集合表示1对多的关 ... -
Hibernate 关联关系 总结
2010-09-09 15:27 9581.一对多的单向关联关系 配置单向的一对多关系是 ... -
Hibernate 关联
2010-09-09 15:24 10011、hibernate多对一关联映 ... -
Hibernate一对多(单向)
2010-09-09 14:31 594[正文]: Hibernate一对多关联,例如一个用户有 ... -
Hibernate中No row with the given identifier exists问题的原因及解决
2010-06-23 09:54 935产生此问题的原因: ... -
Hibernate使用count(*)取得表中记录总数(跨Hibernate3.x版本问题)
2010-06-22 17:13 1530Java代码 /** * @T ... -
Hibernate集合映射
2010-06-13 12:49 912准备找工作,重新整理一下Hibernate,今天做了集合映射 ... -
高并发网站的架构
2010-05-07 11:07 715我在CERNET做过拨号接入平台的搭建,而后在Yaho ... -
Hibernate事务和并发控制
2010-05-07 10:21 9131. 事务介绍:1.1. 事务的定义:事务就 ... -
hibernate中lazy的使用
2009-12-18 22:00 779lazy,延迟加载 Lazy的 ... -
Hibernate中代码自动生成功能小结
2009-12-06 15:10 1035Hibernate中需要class和mapping file, ... -
hibernate工具箱—根据映射文件自动建表
2009-12-04 12:08 976public class ExportDB { ... -
关联加载对象时的报错-----a different object with the same identifier value
2009-11-18 16:13 884因为在hibernate中同一个session里面有了两个相同 ... -
update/saveOrUpdate/merge
2009-11-18 15:28 1140通常下面的场景会使用update()或saveOrUpdate ... -
写得很不错的-Hibernate中的实体状态(二)
2009-11-18 15:08 949(2)session.merge ()方法 ... -
写得很不错的-Hibernate中的实体状态(一)
2009-11-18 15:04 1136持久层的解决方案有许 ... -
Hibernate3.x总结
2009-11-18 14:29 751Hibernate不是盏省油的灯 ... -
Hibernate Annotation几种关联映射
2009-11-02 17:01 1076Hibernate Annotation几种关联映射 一对一 ... -
hibernate3的注解映射学习
2009-11-02 16:41 1345注解映射必须满足两大条件:Hibernate3.2以上版本和J ... -
Hibernate 中级联操作 cascade 选项
2009-11-02 16:35 926none :在保存、更新或删除对象时,忽略其他关联的对象。他是 ...
相关推荐
标题:“Hibernate继承关系映射.pdf” 描述:“简明扼要地介绍了Hibernate中继承关系的映射方式,深入探讨了三种不同的映射策略及其优缺点,同时解析了多态查询的概念。” 知识点: ### Hibernate继承关系映射...
本主题将深入探讨"Hibernate继承映射+C3P0代码"的相关知识点。 首先,让我们理解Hibernate的继承映射。在Java中,我们可以创建一个基类,然后派生出多个子类,这种设计模式在数据库中也可以被映射出来。Hibernate...
映射文件应定义该类的属性与数据库表中的列之间的映射关系。例如,对于`HourlyEmployee`类,映射文件可能如下所示: ```xml ``` 类似的,`SalariedEmployee`类也有其对应的映射文件。 ##### 14.1.2 ...
Hibernate继承映射是将Java中的继承关系映射到数据库的关系模型中。在Java中,一个基类可以有多个子类,而在数据库中,这些子类可以共享一张表或者各自拥有独立的表,这取决于我们选择的继承策略。Hibernate提供了四...
### Hibernate的继承多态映射关系详解 在软件开发领域,特别是使用面向对象编程语言如Java进行开发时,我们经常需要处理复杂的类继承关系。在ORM(Object-Relational Mapping)框架如Hibernate中,如何优雅地将这些...
Hibernate继承映射是将Java类的继承关系映射到数据库表的一种策略,使得对象模型的复杂性能够平滑地转化为关系数据库模型。本篇将详细介绍Hibernate继承映射的第一种策略——每棵类继承树对应一张表,即单一表继承...
总结起来,"每个类都对应一张表"的继承映射策略在Hibernate中是一种直接且易于理解的方法,适合那些每个类都有独特属性的情况。然而,它可能不适合所有场景,特别是当子类众多或者需要减少数据冗余时。在实际应用中...
Hibernate的继承映射 --每个继承层次一张表 -- 每个具体类一张表 -- 每个类一张表
《Hibernate继承映射详解》 在Java开发中,对象关系映射(ORM)框架如Hibernate大大简化了数据库操作。Hibernate不仅提供了对基本数据类型的映射,还支持复杂的数据结构,如继承关系的映射。本篇文章将深入探讨...
**标题:“Hibernate继承映射(Annotation)详解”** 在Java持久化框架Hibernate中,继承映射是一种关键特性,它允许开发者将对象模型的继承结构映射到数据库的表结构。在传统的面向对象编程中,继承是实现代码复用和...
针对hibernate的继承映射部分做了几个测试的例子,更加直观的阐述了继承映射的几种方式以及对应的表结构方式,文件被分割成3部分 http://download.csdn.net/source/259075 http://download.csdn.net/source/259072 ...
针对hibernate的继承映射部分做了几个测试的例子,更加直观的阐述了继承映射的几种方式以及对应的表结构方式,文件被分割成3部分 http://download.csdn.net/source/259075 http://download.csdn.net/source/259072 ...
总结来说,“每个子类一张表”的继承映射策略是Hibernate提供的一种处理继承关系的方法,它将类的继承结构映射到数据库的多个表中。这种策略适合于子类具有大量特有属性的情况,但需要权衡可能带来的数据库设计复杂...