【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()方法:

 

 

posted @ 2017-04-08 17:30  chxbar  阅读(160)  评论(0编辑  收藏  举报