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_ID | TYPE | COLOR | RADIUS | LENGTH | WIDTH | |
1 | 1 | Circle | Red | 2 | (null) | (null) |
2 | 2 | Rectangle | Blue | (null) | 2.5 | 3.5 |
3 | 3 | Shape | Green | (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_ID | COLOR | |
1 | 1 | Red |
2 | 2 | Blue |
3 | 3 | Green |
CIRCLE_ID | RADIUS | |
1 | 1 | 2.0 |
RECT_ID | LENGTH | WIDTH | |
1 | 2 | 3.5 | 3.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_ID | COLOR | |
1 | 3 | Green |
SHAPE_ID | COLOR | RADIUS | |
1 | 1 | Red | 2.0 |
SHAPE_ID | COLOR | LENGTH | WIDTH | |
1 | 2 | Blue | 2.5 | 3.5 |
5. 如果基类是抽象类的话,在class元素中,将abstract属性值设置为true:<class name="Shape" table="tab_shape" abstract="true">...</class>,这样,在TPC映射策略中,将不必使用数据表tab_shape。