【7.6.3】基于每个具体类一张表的继承映射

1.子类和父类的关系图:

 

 

 2.Person,Student和Teacher类

package cn.siggy.pojo;

public class Person {
	private int id;
	private String name;
	private int age;
	/*get/set*/
}

------------------------------------------------------------------
package cn.siggy.pojo;

public class Student extends Person{
	private String work;

	public String getWork() {
		return work;
	}
	public void setWork(String work) {
		this.work = work;
	}
}
-------------------------------------------------------------------

package cn.siggy.pojo;

public class Teacher extends Person{
	private int salary;
	
	public int getSalary() {
		return salary;
	}
	public void setSalary(int salary) {
		this.salary = 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" abstract="true">
		<id name="id" column="id">
			<generator class="assigned"/>
		</id>
		<property name="name"/>
		<property name="age"/>
		<union-subclass name="Student">
			<property name="work"/>
		</union-subclass>
		<union-subclass name="Teacher">
			<property name="salary"/>
		</union-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:23  chxbar  阅读(151)  评论(0编辑  收藏  举报