hibernate继承映射的三类例子,包括Person,Employee,Manager和Customer(zz)
来源:互联网 发布:55度杯性价比 知乎 编辑:程序博客网 时间:2024/06/05 21:13
A、采用subclass元素的继承映射:
采用这种方式,整个继承继承树的所有实例都将保存在同一个表内,因为将父子类的实例全部保存在同一个表内,需要在该表中额外增加一列:使用该列来区分每一行记录到底是哪个类的实例——这个列被称为辨别者
使用subclass来映射子持久化类,使用discriminator元素类映射辨者列,每个类映射中都需要指定辨别列的值!
eg:
Preson.hbm.xml:
<!-- 映射Person类 -->
<class name="Person" discriminator-value="普通人">
<!-- 映射标识属性 -->
<id name="id" column="person_id">
<!-- 使用identity的主键生成器策略 -->
<generator class="identity"/>
</id>
<!-- 映射辨别者列 -->
<discriminator column="wawa" type="string"/>
<!-- 如下映射两个基本属性 -->
<property name="name" length="80"/>
<property name="gender"/>
<!-- 下面映射了一个组件属性 -->
<component name="address">
<!-- 映射组件属性的三个基本属性 -->
<property name="detail"/>
<property name="zip"/>
<property name="country"/>
</component>
<!-- 使用subclass元素映射Person类的子类Employee -->
<subclass name="Employee" discriminator-value="雇员">
<!-- 映射两个基本属性 -->
<property name="title" />
<property name="salary" />
<!-- 映射N-1的关联映射 -->
<many-to-one name="manager" column="manager_id"/>
<!-- 映射与Customer类之间的1:N关联 -->
<set name="customers" inverse="true">
<key column="empoyee_id"/>
<one-to-many class="Customer"/>
</set>
<!-- 使用subclass元素映射Employee类的子类Manager -->
<subclass name="Manager" discriminator-value="经理">
<!-- 映射Manager类的基本属性department -->
<property name="department"/>
<!-- 映射Manager类的关联实体:Employee -->
<set name="employees" inverse="true">
<key column="manager_id"/>
<one-to-many class="Employee"/>
</set>
</subclass>
</subclass>
<!-- 使用subclass映射Person的Customer子类 -->
<subclass name="Customer" discriminator-value="顾客">
<!-- 映射Customer类的comments属性 -->
<property name="comments"/>
<!-- 映射Customer和Employee的关联关系 -->
<many-to-one name="employee" column="empoyee_id"/>
</subclass>
</class>
B、采用joined-subclass元素的继承映射:
采用这种方式,父类实例保存在父类列表里,而子类实例则有父类表和子类表共同存储;
用于将子类和父类共有的属性保存在父类表中,而在子类增加的属性,则保存在子类表中;
必须使用key元素映射父子类的共有主键这些共有主键列的列名不必相同;
eg:
Preson.hbm.xml
<!-- 映射Person类 -->
<class name="Person">
<!-- 映射标识属性 -->
<id name="id" column="person_id">
<!-- 使用identity的主键生成器策略 -->
<generator class="identity"/>
</id>
<!-- 如下映射两个基本属性 -->
<property name="name" length="80"/>
<property name="gender"/>
<!-- 下面映射了一个组件属性 -->
<component name="address">
<!-- 映射组件属性的三个基本属性 -->
<property name="detail"/>
<property name="zip"/>
<property name="country"/>
</component>
<!-- 使用joined-subclass元素映射Person类的Employee子类 -->
<joined-subclass name="Employee">
<!-- 必须使用key元素映射父子类的共有主键 -->
<key column="person_id"/>
<!-- 映射Employee类的两个普通属性 -->
<property name="title" not-null="true"/>
<property name="salary" not-null="true"/>
<!-- 映射Employee类与Manager类之间的N-1关联-->
<many-to-one name="manager" column="manager_id"/>
<!-- 映射Employee类与Customer类之间的1-N关联-->
<set name="customers" inverse="true">
<key column="empoyee_id"/>
<one-to-many class="Customer"/>
</set>
<!-- 使用joined-subclass元素映射Employee类的Manager子类 -->
<joined-subclass name="Manager">
<!-- 必须使用key元素映射父子类的共有主键 -->
<key column="person_id"/>
<!-- 映射Manager类的department属性 -->
<property name="department"/>
<!-- 映射Employee类与Manager类之间的1-N关联-->
<set name="employees" inverse="true">
<key column="manager_id"/>
<one-to-many class="Employee"/>
</set>
</joined-subclass>
</joined-subclass>
<!-- 使用joined-subclass元素映射Person类的Customer子类 -->
<joined-subclass name="Customer">
<!-- 必须使用key元素映射父子类的共有主键 -->
<key column="person_id"/>
<property name="comments" not-null="true"/>
<!-- 映射Employee类与Customer类之间的1-N关联-->
<many-to-one name="employee" column="empoyee_id" not-null="true"/>
</joined-subclass>
</class>
C、采用union-subclass元素的继承映射:
<!-- 映射Person类 -->
<class name="Person">
<!-- 映射标识属性 -->
<id name="id" column="person_id">
<!-- 使用identity的主键生成器策略 -->
<generator class="increment"/>
</id>
<!-- 如下映射两个基本属性 -->
<property name="name" length="80"/>
<property name="gender"/>
<!-- 下面映射了一个组件属性 -->
<component name="address">
<!-- 映射组件属性的三个基本属性 -->
<property name="detail"/>
<property name="zip"/>
<property name="country"/>
</component>
<!-- 使用union-subclass元素映射Person类的Employee子类 -->
<union-subclass name="Employee">
<!-- 映射Employee类的两个普通属性 -->
<property name="title" not-null="true"/>
<property name="salary" not-null="true"/>
<!-- 映射Employee类与Manager类之间的N-1关联-->
<many-to-one name="manager" column="manager_id"/>
<!-- 映射Employee类与Customer类之间的1-N关联-->
<set name="customers" inverse="true">
<key column="empoyee_id"/>
<one-to-many class="Customer"/>
</set>
<!-- 使用union -subclass元素映射Employee类的Manager子类 -->
<union-subclass name="Manager">
<!-- 映射Manager类的department属性 -->
<property name="department"/>
<!-- 映射Employee类与Manager类之间的1-N关联-->
<set name="employees" inverse="true">
<key column="manager_id"/>
<one-to-many class="Employee"/>
</set>
</union-subclass>
</union-subclass>
<!-- 使用union -subclass元素映射Person类的Customer子类 -->
<union-subclass name="Customer">
<!-- 映射Customer的普通属性 -->
<property name="comments" not-null="true"/>
<!-- 映射Employee类与Customer类之间的1-N关联-->
<many-to-one name="employee" column="empoyee_id"
not-null="true"/>
</union-subclass>
</class>
转自:http://younglibin.javaeye.com/blog/462665
- hibernate继承映射的三类例子,包括Person,Employee,Manager和Customer(zz)
- Manager 继承 Employee 的举例
- Hibernate映射Person类
- 《JAVA继承与多态》【Person、Student、Employee类】(注:此题在书上原题基础上有修改)设计一个名为Person的类和它的两个名为Student和Employee子类。
- 实现一个名为Person的类和它的两个子类Student和Employee。
- Hibernate继承映射的三种方式
- Hibernate的三种继承映射
- Hibernate 映射和继承
- hibernate的继承的映射关系的例子
- hibernate映射继承关系三
- Person Student Employee Faculty Staff 类
- Hibernate的继承映射
- Hibernate的继承映射
- Hibernate的继承映射
- hibernate的继承映射
- 三、Hibernate组件映射与继承映射
- hibernate的继承类的映射
- Hibernate三种继承映射方式的比较
- andriod 学习笔记一
- GAE 博客——B3log Solo 0.2.1 发布了!
- 接收post数据时汉字乱码问题
- tar 打包压缩目录
- 一个IT人士的个人经历,给迷失方向的朋友
- hibernate继承映射的三类例子,包括Person,Employee,Manager和Customer(zz)
- 如何选择安全漏洞扫描工具
- 在DLL编程中调用模版类时出现的类似"class“XXX”需要有 dll 接口由 class“XXX”的客户端使用"的warning的解决方案
- SQL手工注入方法总结(私人收藏)
- 调出隐藏事件OnPaint的方法
- 有关SelectedValue,SelectedItem,Text
- JQuery_JAVA_利用jQuery的AJAX和JSON实现可输入下拉框提示
- nagios 配置 短信飞信邮件告警
- VPN连接公司内联网实现家庭办公