Hibernate继承映射的简单示例
来源:互联网 发布:数据库中substring 编辑:程序博客网 时间:2024/05/16 12:22
1. 继承关系:
基类:Shape
子类:Circle,Rectangle均继承Shape
package com.huey.entity;/** * 图形实体 * @author Huey2672 * */public class Shape {private Integer id;private String color;public Integer getId() {return id;}public void setId(Integer id) {this.id = id;}public String getColor() {return color;}public void setColor(String color) {this.color = color;}public Shape() {}public Shape(String color) {super();this.color = color;}}
package com.huey.entity;/** * 圆形实体 * @author Huey2672 * */public class Circle extends Shape {private Double radius;public Double getRadius() {return radius;}public void setRadius(Double radius) {this.radius = radius;}public Circle() {super();}public Circle(String color, Double radius) {super(color);this.radius = radius;}}
package com.huey.entity;/** * 矩形实体 * @author Huey2672 * */public class Rectangle extends Shape {private Double length;private Double width;public Double getLength() {return length;}public void setLength(Double length) {this.length = length;}public Double getWidth() {return width;}public void setWidth(Double width) {this.width = width;}public Rectangle() {super();}public Rectangle(String color, Double length, Double width) {super(color);this.length = length;this.width = width;}}
2. TPH(Table Per Hierarchy)映射策略,每棵继承树使用一张表,Shape.hbm.xml:
<?xml version="1.0"?><!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"><hibernate-mapping package="com.huey.entity"><!-- 映射持久化类Shape,discriminator-value指定辨别者列值 --><class name="Shape" table="tab_shape" discriminator-value="Shape"><id name="id" column="shape_id"><generator class="increment"></generator></id><!-- 指定映射辨别者列,其类型指定为string --><discriminator column="type" type="string"/><property name="color" column="color"/><!-- subclass映射子持久化类Circle,discriminator-value指定辨别者列值 --><subclass name="Circle" discriminator-value="Circle"><property name="radius" column="radius"/></subclass><!-- subclass映射子持久化类Rectangle,discriminator-value指定辨别者列值 --><subclass name="Rectangle" discriminator-value="Rectangle"><property name="length" column="length"/><property name="width" column="width"/></subclass></class></hibernate-mapping>数据表结构:
tab_shape
SHAPE_IDTYPECOLORRADIUSLENGTHWIDTH11CircleRed2(null)(null)22RectangleBlue(null)2.53.533ShapeGreen(null)(null)(null)3. TPS(Table Per Subclass)映射策略,每个子类都有一张表,Shape.hbm.xml:
<?xml version="1.0"?><!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"><hibernate-mapping package="com.huey.entity"><!-- 映射持久化类Shape --><class name="Shape" table="tab_shape"><id name="id" column="shape_id"><generator class="increment"></generator></id><property name="color" column="color"/><!-- joined-subclass映射子持久化类Circle --><joined-subclass name="Circle" table="tab_circle"><!-- 必须使用key元素映射父子类的共有主键 --><key column="circle_id"/><property name="radius" column="radius"/></joined-subclass><!-- joined-subclass映射子持久化类Rectangle --><joined-subclass name="Rectangle" table="tab_rect"><key column="rect_id"/><property name="length" column="length"/><property name="width" column="width"/></joined-subclass></class></hibernate-mapping>
数据表结构:
tab_shape
SHAPE_IDCOLOR11Red22Blue33Greentab_circle CIRCLE_IDRADIUS112.0tab_rect RECT_IDLENGTHWIDTH123.53.54. TPC(Table Per Concrete Class)映射策略,每个具体类使用一张表,Shape.hbm.xml:
<?xml version="1.0"?><!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"><hibernate-mapping package="com.huey.entity"><!-- 映射持久化类Shape --><class name="Shape" table="tab_shape"><id name="id" column="shape_id"><generator class="increment"></generator></id><property name="color" column="color"/><!-- union-subclass映射子持久化类Circle --><union-subclass name="Circle" table="tab_circle"><property name="radius" column="radius"/></union-subclass><!-- union-subclass映射子持久化类Rectangle --><union-subclass name="Rectangle" table="tab_rect"><property name="length" column="length"/><property name="width" column="width"/></union-subclass></class></hibernate-mapping>
数据表结构:
tab_shape
SHAPE_IDCOLOR13Greentab_circle SHAPE_IDCOLORRADIUS11Red2.0tab_rect SHAPE_IDCOLORLENGTHWIDTH12Blue2.53.55. 如果基类是抽象类的话,在class元素中,将abstract属性值设置为true:<class name="Shape" table="tab_shape" abstract="true">...</class>,这样,在TPC映射策略中,将不必使用数据表tab_shape。
- Hibernate继承映射的简单示例
- SSH框架之Hibernate的组件映射、简单继承映射
- hibernate的继承映射 之 三种示例
- Hibernate继承映射简单入门
- Hibernate的继承映射
- Hibernate的继承映射
- Hibernate的继承映射
- hibernate的继承映射
- Hibernate基础------》关系映射---》继承映射-》简单映射
- Hibernate继承映射,数据表的继承
- Hibernate的简单示例
- 简单的Java继承示例!
- hibernate的继承类的映射
- hibernate不支持这样的继承映射
- Hibernate继承映射时错误的解决方法
- Hibernate继承映射的两种策略
- Hibernate笔记=>继承关系的映射
- Hibernate继承映射的三种方式
- 多线程
- 服务器环境
- java反射及应用
- 分治法排序
- paip.提升用户体验-----c++ gcc 命令在notepad++扩展中的配置..
- Hibernate继承映射的简单示例
- android应用中获取文件名的两个方法
- Html5学习笔记 2 selectors api
- 将对象转化为json格式 反序列化处理
- Hibernate一对多单向关联,inverse和cascade的区别
- 递归二叉树建立、遍历、删除、打印
- hashtable和hashmap解析
- Struts2整合JFreeChart图表---------------JFreeChart作图
- android actionbar的神奇问题