Hibernate继承映射
来源:互联网 发布:java解释器是什么 编辑:程序博客网 时间:2024/06/07 04:06
Hibernate继承映射
(2012-10-27 11:59:08)继承映射
分类: Hibernate第一种方案:一个子类(全部属性含继承过来的)对应一张表
第二种方案:使用一张表表示所有继承体系下的类的属性的并集
第三种方案:每个子类使用一张表只存储它特有的属性,然后与父类所对应的表以一对一主键关联的方式关联起来。
第四中方案:混合使用“一个类继承体系一张表”和“每个子类一张表”
现在假设有People、Student、Teacher三个类,父类为People,Student与Teacher为People的父类,代码如下:
People类:
public class People
{
}
Student类:
public class Student extends People{
}
Teacher类:
public class Teacher extends People{private int salary;//工资 public int getSalary() { return salary; } public void setSalary(int salary) { this.salary = salary; }}
第一种方案:一个子类(全部属性含继承过来的)对应一张表
该方案是使继承体系中每一个子类都对应数据库中的一张表。示意图如下:
每一个子类对应的数据库表都包含了父类的信息,并且包含了自己独有的属性。每个子类对应一张表,而且这个表的信息是完备的,即包含了所有从父类继承下来的属性映射的字段。这种策略是使用<union-subclass>标签来定义子类的。
配置People.hbm.xml文件:
<?xml version="1.0" encoding="utf-8"?><!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"><hibernate-mapping><class name="com.suxiaolei.hibernate.pojos.People" abstract="true"> <id name="id" type="string"> <column name="id"></column> <generator class="uuid"></generator> </id> <property name="name" column="name" type="string"></property> <property name="sex" column="sex" type="string"></property> <property name="age" column="age" type="string"></property> <property name="birthday" column="birthday" type="timestamp"></property> <!-- <union-subclass name="com.suxiaolei.hibernate.pojos.Student" table="student"> <property name="cardId" column="cardId" type="string"></property> </union-subclass> <union-subclass name="com.suxiaolei.hibernate.pojos.Teacher" table="teacher"> <property name="salary" column="salary" type="integer"></property> </union-subclass> --> </class> <union-subclass name="com.suxiaolei.hibernate.pojos.Student" table="student" extends="com.suxiaolei.hibernate.pojos.People"> <property name="cardId" column="cardId" type="string"></property> </union-subclass> <union-subclass name="com.suxiaolei.hibernate.pojos.Teacher" table="teacher" extends="com.suxiaolei.hibernate.pojos.People"> <property name="salary" column="salary" type="integer"></property> </union-subclass></hibernate-mapping>
以上配置是一个子类一张表方案的配置,<union-subclass>标签是用于指示出该hbm文件所表示的类的子类,如 People类有两个子类,就需要两个<union-subclass>标签以此类推。<union-subclass>标签 的"name"属性用于指定子类的全限定名称,"table"属性用于指定该子类对应的表的名称,"extends"属性用于指定该子类的父类,注意该属 性与<union-subclass>标签的位置有关,若 <union-subclass>标签作为<class>标签的子标签,则"extends"属性可以不设置,否则需要明确设 置"extends"属性。<class>标签中的"abstract"属性如果值为true则,不会生成表结构。如果值为false则会生 成表结构,但是不会插入数据。
主健不能是identity类型,如果父类是abstract=”true”就不会有表与之对应。
隐式多态,映射文件没有联系,限制比较多很少使用。
继承实现建议:表的数量不能高于类的数量
根据People.hbm.xml生成表结构:
drop table if exists student
可以看到一个子类对应一张表。
第二种方案:使用一张表表示所有继承体系下的类的属性的并集
这种策略是使用<subclass>标签来实现的。因为类继承体系下会有许多个子类,要把多个类的信息存放在一张表中,必须有某种机制 来区分哪些记录是属于哪个类的。Hibernate中的这种机制就是,在表中添加一个字段,用这个字段的值来进行区分。在表中添加这个标示列使 用<discriminator>标签来实现。
该策略的示意图:
将继承体系中的所有类信息表示在同一张表中后,只要是这个类没有的属性会被自动赋上null。
配置People.hbm.xml:
<?xml version="1.0" encoding="utf-8"?><!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"><hibernate-mapping><class name="com.suxiaolei.hibernate.pojos.People" table="people"> <id name="id" type="string"> <column name="id"></column> <generator class="uuid"></generator> </id> <discriminator column="peopleType" type="string"></discriminator> <property name="name" column="name" type="string"></property> <property name="sex" column="sex" type="string"></property> <property name="age" column="age" type="string"></property> <property name="birthday" column="birthday" type="timestamp"></property> <subclass name="com.suxiaolei.hibernate.pojos.Student" discriminator-value="student"> <property name="cardId" column="cardId" type="string"></property> </subclass> <subclass name="com.suxiaolei.hibernate.pojos.Teacher" discriminator-value="teacher"> <property name="salary" column="salary" type="string"></property> </subclass> </class></hibernate-mapping><discriminator>标签用于在表中创建一个标识列,其"column"属性指定标识列的列名,"type"指定了标识列的 类型。<subclass>标签用于指定该HBM文件代表类的子类,有多少子类就有多少个该标签,其"name"属性指定子类的名 称,"discriminator-value"属性指定该子类的数据的标识列的值是什么,其"extends"属性与<union- subclass>的"extends"属性用法一致。
注意:<discriminator column="peopleType" type="string"></discriminator>的定义一定要紧挨着id元素的下方,否则错误
根据People.hbm.xml生成表结构:
drop table if exists peoplecreate table people ( id varchar(255) not null, peopleType varchar(255) not null, name varchar(255), sex varchar(255), age varchar(255), birthday datetime, cardId varchar(255), salary varchar(255), primary key (id) )
可以看到一张表将继承体系下的所有信息都包含了,其中"peopleType"为标识列。
第三种方案:每个子类使用一张表只存储它特有的属性,然后与父类所对应的表以一对一主键关联的方式关联起来。
这种策略是使用<joined-subclass>标签来定义子类的。父类、子类都对应一张数据库表。在父类对应的数据库表中,它存储 了所有记录的公共信息,实际上该父类对应的表会包含所有的记录,包括父类和子类的记录;在子类对应的数据库表中,这个表只定义了子类中所特有的属性映射的 字段。子类对应的数据表与父类对应的数据表,通过一对一主键关联的方式关联起来。
这种策略的示意图:
people表中存储了子类的所有记录,但只记录了他们共有的信息,而他们独有的信息存储在他们对应的表中,一条记录要获得其独有的信息,要通过people记录的主键到其对应的子表中查找主键值一样的记录然后取出它独有的信息。
配置People.hbm.xml:
<?xml version="1.0" encoding="utf-8"?><!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"><hibernate-mapping><class name="com.suxiaolei.hibernate.pojos.People" table="people"> <id name="id" type="string"> <column name="id"></column> <generator class="uuid"></generator> </id> <property name="name" column="name" type="string"></property> <property name="sex" column="sex" type="string"></property> <property name="age" column="age" type="string"></property> <property name="birthday" column="birthday" type="timestamp"></property> <joined-subclass name="com.suxiaolei.hibernate.pojos.Student" table="student"> <key column="id"></key> <property name="cardId" column="cardId" type="string"></property> </joined-subclass> <joined-subclass name="com.suxiaolei.hibernate.pojos.Teacher" table="teacher"> <key column="id"></key> <property name="salary" column="salary" type="integer"></property> </joined-subclass> </class></hibernate-mapping>
<joined-subclass>标签需要包含一个key标签,这个标签指定了子类和父类之间是通过哪个字段来关联的。
根据People.hbm.xml生成表结构:
drop table if exists people
可以看到,父类对应的表保存公有信息,子类对应的表保存独有信息,子类和父类对应的表使用一对一主键关联的方式关联起来。
第四中方案:混合使用“一个类继承体系一张表”和“每个子类一张表”<class name="Employee" table="employee" discriminator-value="0" >
</class>
- Hibernate高级映射 --- 继承映射
- Hibernate高级映射--继承映射
- Hibernate高级映射-继承映射
- Hibernate映射解析---继承映射
- Hibernate映射解析---继承映射
- Hibernate映射解析---继承映射
- Hibernate 简化继承映射
- Hibernate 简化继承映射
- Hibernate 简化继承映射
- Hibernate 简化继承映射
- Hibernate 简化继承映射
- Hibernate 简化继承映射
- Hibernate的继承映射
- hibernate继承映射2
- Hibernate 继承关系映射
- hibernate继承映射
- hibernate总结---继承映射
- Hibernate继承映射(1)
- ng-repeat 中动态绑定ng-click 点击事件处理函数&动态改变ng-class
- 安卓内存泄露分析整理
- 机器学习常见算法总结(面试用)
- c语言实例之矩阵转置
- Hibernate基本配置
- Hibernate继承映射
- Java面向对象概述
- iOS pushcontroller时自动隐藏tabbar
- 预装64位Win8/8.1电脑安装64位Win7详细过程(单/双系统)
- 机器学习中过拟合问题分析及解决方法
- C++设计模式浅识装饰模式
- 工厂模式三部曲-简单工厂模式
- 金额转大写
- Redis应用案例 查找某个值的范围