hibernate继承映射2

本文介绍了一种使用 Hibernate 的 union-subclass 映射策略来实现继承关系的方法,通过示例展示了如何定义 Animal 基类及其子类 Cat 和 Monkey,并配置相应的 hibernate 映射文件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

       上一篇博客的继承映射所用到的节点是joined-subclass节点,在数据库端生成的是三个表,而该篇所用到的节点是union-subclass节点,生成的表只有两个。但此种方法生成的表的主键不能进行自增长。

1、创建实体类Animal.java

package cn.itcast.b;

public class Animal {
	private String id;
	private String name;

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}
	
	
	
}

2、创建实体类Cat.java继承于Animal类

package cn.itcast.b;

public class Cat extends Animal{
	private String catchMouse;

	public String getCatchMouse() {
		return catchMouse;
	}

	public void setCatchMouse(String catchMouse) {
		this.catchMouse = catchMouse;
	}

	
	
}


3、创建实体类Monkey.java继承于Animal类

package cn.itcast.b;

public class Monkey extends Animal{
	private String eatBanana;

	public String getEatBanana() {
		return eatBanana;
	}

	public void setEatBanana(String eatBanana) {
		this.eatBanana = eatBanana;
	}
	
}

4、配置Aminal.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="cn.itcast.b">
	<!-- abstract="true"指定实体类对象不对应表,即在数据库端不生成表 -->
	<class name="Animal" abstract="true">
	<!-- 如果用union-subclass节点,主键生成策略不能为自增长-->
		<id name="id">
			<generator class="uuid"></generator>
		</id>
		<!-- 其他字段 -->
		<property name="name" length="20"></property>
		<!-- 
			子类:猫  t_cat
			union-subclass  
				table 指定为表名, 表的主键即为id列
		-->
		<union-subclass name="Cat" table="t_cat">
			<property name="catchMouse"></property>
		</union-subclass>
		<!-- 子类:猴子  t_monkey -->
		<union-subclass name="Monkey" table="t_monkey">
			<property name="eatBanana"></property>
		</union-subclass>	
	</class>
</hibernate-mapping>

5、配置hibernate.cfg.xml

<!DOCTYPE hibernate-configuration PUBLIC
	"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
	"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
	<session-factory>
		<property name="show_sql">true</property>
		<!-- 数据库连接配置 -->
		<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
		<property name="hibernate.connection.url">jdbc:mysql:///employee</property>
		<property name="hibernate.connection.username">root</property>
		<property name="hibernate.connection.password">123456</property>
		<property name="dialect">org.hibernate.dialect.MySQL5Dialect</property>
		<!-- 其他相关配置 -->
		<!-- 显示hibernate在运行时执行的sql语句 -->
		<property name="hibernate.show_sql">true</property>
		<!-- 格式化sql -->		
		<property name="hibernate.format_sql">true</property>
		<!-- 自动建表 -->
		<property name="hibernate.hbm2ddl.auto">update</property>
		<!-- 加载所有映射 -->
		<mapping resource="cn/itcast/b/Animal.hbm.xml"/>
	</session-factory>
</hibernate-configuration>

6、创建测试类App.java

package cn.itcast.b;

import static org.junit.Assert.*;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.junit.Test;

public class App {
	private static SessionFactory sf;
	static {
		sf=new Configuration().configure().buildSessionFactory();
	}
	@Test
	public void test() {
		Session session=sf.openSession();
		session.beginTransaction();
		
		Cat cat = new Cat();
		cat.setName("小猫");
		cat.setCatchMouse("抓猫");
		
		Monkey monkey = new Monkey();
		monkey.setName("猴子");
		monkey.setEatBanana("吃香蕉");
		
		session.save(monkey);
		session.save(cat);
		
		session.getTransaction().commit();
		session.close();
	}

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值