【7.6.4】基于每个类一张表的继承映射

1.类之间的关系

 

2.Person类,Student类,Teacher类

public class Person {
	private int id;
	private String name;
	private int age;
}
====================
public class Student extends Person{
	private String work;
}
=================

public class Teacher extends Person{
	private int salary;
}

  

3.映射文件

Person.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.siggy.pojo">
	<class name="Person">
		<id name="id" column="id">
			<generator class="assigned"></generator>
		</id>
		<property name="name"/>
		<property name="age"/>
		<joined-subclass name="Teacher">
			<key column="id"/>
			<property name="salary"/>
		</joined-subclass>
		<joined-subclass name="Student">
			<key column="id"/>
			<property name="work"/>
		</joined-subclass>
	</class>
</hibernate-mapping>

  

4.测试代码

package cn.siggy.test;

import java.sql.SQLException;

import javax.sql.rowset.serial.SerialException;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.tool.hbm2ddl.SchemaExport;
import org.junit.Test;

import cn.siggy.pojo.Person;
import cn.siggy.pojo.Student;
import cn.siggy.pojo.Teacher;
import cn.siggy.util.HibernateUtil;

public class HibernateTest {
	@Test
	public void testCreateDB(){
		Configuration cfg = new Configuration().configure();
		SchemaExport se = new SchemaExport(cfg);
		//第一个参数 是否生成ddl脚本  第二个参数  是否执行到数据库中
		se.create(true, true);
	}
	@Test
	public void testSave() throws HibernateException, SerialException, SQLException{
		Configuration cfg = new Configuration().configure();
		SessionFactory factory = cfg.buildSessionFactory(new StandardServiceRegistryBuilder()
		.applySettings(cfg.getProperties()).build());
		Session session = null;
		Transaction tx = null;
		try{
			session = factory.openSession();
			tx = session.beginTransaction();
			Teacher teacher = new Teacher();
			teacher.setId(1);
			teacher.setName("siggy");
			teacher.setAge(26);
			teacher.setSalary(5000);
			Student student = new Student();
			student.setId(2);
			student.setName("小明");
			student.setAge(22);
			student.setWork("hello world");
			Student student1 = new Student();
			student1.setId(3);
			student1.setName("小强");
			student1.setAge(20);
			student1.setWork("struts2");
			session.save(student);
			session.save(student1);
			session.save(teacher);
			tx.commit();
			
		}catch (HibernateException e) {
			if(tx!=null)
				tx.rollback();
			e.printStackTrace();
			throw e;
		}finally{
			HibernateUtil.closeSession();
		}
	}
	@Test
	public void testGet(){
		Session session = null;
		Transaction tx = null;
		try{
			session = HibernateUtil.getSession();
			tx = session.beginTransaction();
			Person person = (Person)session.get(Person.class, 2);
			System.out.println(person.getName());
			if(person instanceof Student){
				Student stu = (Student)person;
				System.out.println(stu.getWork());
			}
			tx.commit();
		}catch (HibernateException e) {
			if(tx!=null)
				tx.rollback();
			e.printStackTrace();
			throw e;
		}finally{
			HibernateUtil.closeSession();
		}
	}
	@Test
	public void testLoad(){
		Session session = null;
		Transaction tx = null;
		try{
			session = HibernateUtil.getSession();
			tx = session.beginTransaction();
			Person person = (Person)session.load(Person.class, 2);
			System.out.println(person.getName());
			if(person instanceof Student){
				Student stu = (Student)person;
				System.out.println(stu.getWork());
			}
			tx.commit();
		}catch (HibernateException e) {
			if(tx!=null)
				tx.rollback();
			e.printStackTrace();
			throw e;
		}finally{
			HibernateUtil.closeSession();
		}
	}
}

  

5.测试结果

1.执行testSave()方法:

 

 

转载于:https://www.cnblogs.com/chxbar/p/6682394.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值