Hibernate PER Table 继承方式

一、介绍

映射说明:
    1,使用per-table的方式,最重要的是要注意根对象的主键生成策略,一定是需要hibernate来管理的主键生成策略;
    2,所有的子类使用union-subclass来映射;
    3,在union-subclass上使用table属性为每一个子类指定对应的表名称;

关于继承的选择:
    1,在实际的项目开发中,一般很少使用继承的方式;
    2,如果真的需要使用到继承,请使用PER TABLE的方式;
    3,其实,在真实项目中,如果有继承的需要,也是使用many2one,或者one2one来完成的;使用组合来代替继承;

二、实体类关系


@Setter
@Getter
public class BookProduct extends  Product {
    private String isbn;
    private String author;
}
 
 
@Setter
@Getter
public class ClothProduct extends  Product{
    private String size;
    private String color;
}
 
@Setter
@Getter
public class Product {
    private Long id;
    private String name;
 
}

三、配置文件

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping SYSTEM 
 "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd" >

<hibernate-mapping  package="com.shenzhenair.day03.extend" >
	<class name="Product"  >
		<id name="id"  >
			<generator class="increment"/>
		</id>
		<property name="name" />

		<union-subclass name="BookProduct"  table="bookproduct" >
			<property name="isbn" />
			<property name="author" />
		</union-subclass>

		<union-subclass name="ClothProduct"  table="clothproduct" >
			<property name="size" />
			<property name="color" />
		</union-subclass>
	</class>

</hibernate-mapping>

猜你喜欢

转载自blog.csdn.net/m0_38068812/article/details/83869816
今日推荐