Hibernate继承映射的简单示例

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_IDTYPECOLORRADIUSLENGTHWIDTH
11CircleRed2(null)(null)
22RectangleBlue(null)2.53.5
33ShapeGreen(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_IDCOLOR
11Red
22Blue
33Green
tab_circle

 CIRCLE_IDRADIUS
112.0
tab_rect

 RECT_IDLENGTHWIDTH
123.53.5

4. 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_IDCOLOR
13Green
tab_circle

 SHAPE_IDCOLORRADIUS
11Red2.0
tab_rect

 SHAPE_IDCOLORLENGTHWIDTH
12Blue2.53.5

5. 如果基类是抽象类的话,在class元素中,将abstract属性值设置为true:<class name="Shape" table="tab_shape" abstract="true">...</class>,这样,在TPC映射策略中,将不必使用数据表tab_shape。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值