Hibernate 简单使用(七)继承映射的几种方式

在继承映射中~有3种方式可以实现继承映射~

第一种:每个类一张表

在表中的关系--子类的主键引用父类的主键,保证父子类的id一致

第二种:一个类域一张表

在表中的关系--父子类共用一张表,这张表中包含了父子类的所有字段


第三种方式:每个子类一张表


POJo类如下~

Animal类:

public class Animal {
	private String id;
	private String name;
	
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
}
Dog类:

public class Dog extends Animal {
	private String color;//颜色

	public String getColor() {
		return color;
	}

	public void setColor(String color) {
		this.color = color;
	}

	@Override
	public String toString() {
		return "Dog [color=" + color + ", getId()=" + getId() + ", getName()="
				+ getName() + "]";
	}
}
Bird类:

public class Bird extends Animal {
	private String type;//品种

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}
}

第一种方式:

在映射文件中的关系,配置在同一个映射文件中

写映射文件inherit_per_class.hbm.xml:

<?xml version="1.0" encoding="UTF-8"?>
<!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.xu.day5.inherit">
    <class name="Animal" table="tbl_animal">
        <id name="id" column="id">
            <generator class="uuid.hex"/>
        </id>
        <property name="name"/>
        <!-- 以上是配置父类的基本属性,需要指定父类使用的表名-->
<!-- 以上是配置子类的配置,需要指定子类使用的表名,
使用joined-subclass来和父类关联,key值继承父类的,
column指定子类在表中的主键;property为子类特有的属性-->
		<joined-subclass name="Dog" table="tbl_dog">
			<key column="id"/>
			<property name="color"/>
		</joined-subclass>      
		<joined-subclass name="Bird" table="tbl_bird">
			<key column="id"/>
			<property name="type"/>
		</joined-subclass>         
    </class>
</hibernate-mapping>

第二种方式:

在映射文件中的关系,配置在同一个映射文件中

写映射文件inherit_son_class.hbm.xml:

<?xml version="1.0" encoding="UTF-8"?>
<!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.xu.day5.inherit">
    <class name="Animal" abstract="true">
        <id name="id" column="id">
            <generator class="uuid.hex"/>
        </id>
       <!-- discriminator用来指定标识列 -->
	<discriminator column="a_value"/>
	<property name="name"/>
<!-- discriminator-value用来指定标识列中应该填入的值,
用这个值来区分这条记录是属于哪个类的,
property用来指明子类特有的属性 -->
		<union-subclass name="Dog" table="tbl_dog">
			<property name="color"/>
		</union-subclass>
		<union-subclass name="Bird" table="tbl_bird">
			<property name="type"/>
		</union-subclass>
    </class>
</hibernate-mapping>

第三种方式:

在映射文件中的关系,配置在同一个映射文件中

写映射文件inherit_one_class.hbm.xml:

<?xml version="1.0" encoding="UTF-8"?>
<!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.xu.day5.inherit">
    <class name="Animal">
        <id name="id">
            <generator class="native"/>
        </id>
        <property name="name"/>
        <property name="color"/>
        <union-subclass name="Dog" table="dog">
            <property name="callType"></property>
        </union-subclass>
        <union-subclass name="Bird" table="bird">
        	<property name="type"></property>
        </union-subclass>
</class>
</hibernate-mapping>


然后在集成到配置文件中~

		<mapping resource="com/xu/day5/inherit/inherit_per_class.hbm.xml"/>
		<mapping resource="com/xu/day5/inherit/inherit_son_class.hbm.xml"/>
		<mapping resource="com/xu/day5/inherit/inherit_one_class.hbm.xml"/>

测试类:

public class Test {
	public static void main(String[] args) {
		Session session = HibernateSessioFactory.getSession();
		Transaction tran = session.beginTransaction();
		
		Dog dog = new Dog();
		dog.setColor("金黄色");
		dog.setName("一休");
		
		Bird bird = new Bird();
		bird.setName("bage");
		bird.setType("八哥鸟");
		session.save(bird);
		session.save(dog);
		
		tran.commit();
	}
}
效果图如下:




这里有些代码没写出来。可以去我前几篇文章看看~

其实这个继承映射还是蛮简单的~


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值