hibernate继承映射

一.

Hibernate支持三种继承映射策略:
使用 subclass 进行映射:将域模型中的每一个实体对象映射到一个独立的表中,也就是说不用在关系数据模型中考虑域模型中的继承关系和多态。 
使用 joined-subclass 进行映射: 对于继承关系中的子类使用同一个表,这就需要在数据库表中增加额外的区分子类类型的字段。 
使用  union-subclass 进行映射:域模型中的每个类映射到一个表,通过关系数据模型中的外键来描述表之间的继承关系。这也就相当于按照域模型的结构来建立数据库中的表,并通过外键来建立表之间的继承关系。


注意区别:


二.采用 subclass 元素的继承映射


代码实现:

1.Person

package cn.edu.sdut.hibernate.subclass;

public class Person{

	private int id;
	private String name;
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	
}
2.Student
package cn.edu.sdut.hibernate.subclass;

public class Student extends Person {

	private String school;

	public String getSchool() {
		return school;
	}

	public void setSchool(String school) {
		this.school = school;
	}
	
}

3.Person.hbm.xml

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated 2016-11-1 15:08:12 by Hibernate Tools 3.4.0.CR1 -->
<hibernate-mapping>
    <class name="cn.edu.sdut.hibernate.subclass.Person" table="PERSONS" <span style="color:#ff0000;"><span style="color:#ff0000;">discriminator-value="person"</span></span>>
        <id name="id" type="int">
            <column name="ID" />
            <generator class="native" />
        </id>
        
     <span style="color:#ff0000;"><span style="color:#ff0000;">   <!-- 配置辨别者列 -->
        <discriminator column="TYPE" type="string"></discriminator></span>
        
        <property name="name" type="java.lang.String">
            <column name="NAME" />
        </property>
        
       <span style="color:#ff0000;"> <!-- 映射子类 Student, 使用 subclass 进行映射 -->
        <subclass name="cn.edu.sdut.hibernate.subclass.Student" discriminator-value="student">
        	<property name="school" type="string" column="SCHOOL"></property>
        </subclass></span></span>
    </class>
</hibernate-mapping>
4.TestSubClass
package cn.edu.sdut.hibernate.subclass;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;



public class TestSubClass {

	private SessionFactory sessionFactory;
	private Session session;
	private Transaction transaction;
	
	@Before
	public void init(){
		Configuration configuration = new Configuration().configure();
		ServiceRegistry serviceRegistry = 
				new ServiceRegistryBuilder().applySettings(configuration.getProperties())
											.buildServiceRegistry();
		sessionFactory = configuration.buildSessionFactory(serviceRegistry);
		session = sessionFactory.openSession();
		transaction = session.beginTransaction();
	}
	@After
	public void destory(){
		transaction.commit();
		session.close();
		sessionFactory.close();
	}
	
<span style="white-space:pre">	</span>/**
<span style="white-space:pre">	</span> * 缺点:
<span style="white-space:pre">	</span> * 1. 使用了辨别者列.
<span style="white-space:pre">	</span> * 2. 子类独有的字段不能添加非空约束.
<span style="white-space:pre">	</span> * 3. 若继承层次较深, 则数据表的字段也会较多. 
<span style="white-space:pre">	</span> */
<span style="white-space:pre">	</span>
<span style="white-space:pre">	</span>/**
<span style="white-space:pre">	</span> * 插入操作: 
<span style="white-space:pre">	</span> * 1. 对于子类对象只需把记录插入到一张数据表中.
<span style="white-space:pre">	</span> * 2. 辨别者列有 Hibernate 自动维护. 
<span style="white-space:pre">	</span> */
	@Test
	public void testSave(){
		Person person = new Person();
		person.setName("aa");
		session.save(person);
		
		Student student = new Student();
		student.setSchool("jianqiao");
		session.save(student);
	}
	
<span style="white-space:pre">	</span>/**
<span style="white-space:pre">	</span> * 查询:
<span style="white-space:pre">	</span> * 1. 查询父类记录, 只需要查询一张数据表
<span style="white-space:pre">	</span> * 2. 对于子类记录, 也只需要查询一张数据表
<span style="white-space:pre">	</span> */
	@Test
	public void testQuery(){
<span style="white-space:pre">		</span>//<span style="color:#ff0000;">Person是类</span>
		List<Person> list = session.createQuery(<span style="color:#ff0000;">"from Person"</span>).list();
		System.out.println(list);
		
		List<Student> list2 = session.createQuery("from Student").list();
		System.out.println(list2);
	}
}

三.采用 joined-subclass 元素的继承映射

1.Person

package cn.edu.sdut.hibernate.joined_subclass;

public class Person{

	private int id;
	private String name;
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	
}

2.Student
package cn.edu.sdut.hibernate.joined_subclass;

public class Student extends Person {

	private String school;

	public String getSchool() {
		return school;
	}

	public void setSchool(String school) {
		this.school = school;
	}
	
}
3.Person.hbm.xml

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated 2016-11-1 15:08:12 by Hibernate Tools 3.4.0.CR1 -->
<hibernate-mapping auto-import="false">
    <class name="cn.edu.sdut.hibernate.joined_subclass.Person" table="PERSONS" >
        <id name="id" type="int">
            <column name="ID" />
            <generator class="native" />
        </id>
        
       <property name="name" type="java.lang.String">
            <column name="NAME" />
        </property>
        
         <!-- 映射子类 Student, 使用 joined-subclass 进行映射 -->
        <joined-subclass name="cn.edu.sdut.hibernate.joined_subclass.Student" table="STUDENT">
        	<!-- STUDENT_ID是随着person的主键建立的 -->
        	<key column="STUDENT_ID"></key>
        	<property name="school" type="string" column="SCHLLO"></property>
        </joined-subclass>
    </class>
</hibernate-mapping>

4.TestSubClass
package cn.edu.sdut.hibernate.joined_subclass;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;



public class TestSubClass {

	private SessionFactory sessionFactory;
	private Session session;
	private Transaction transaction;
	
	@Before
	public void init(){
		Configuration configuration = new Configuration().configure();
		ServiceRegistry serviceRegistry = 
				new ServiceRegistryBuilder().applySettings(configuration.getProperties())
											.buildServiceRegistry();
		sessionFactory = configuration.buildSessionFactory(serviceRegistry);
		session = sessionFactory.openSession();
		transaction = session.beginTransaction();
	}
	@After
	public void destory(){
		transaction.commit();
		session.close();
		sessionFactory.close();
	}
	
	/**
	 * 优点:
	 * 1. 不需要使用了辨别者列.
	 * 2. 子类独有的字段能添加非空约束.
	 * 3. 没有冗余的字段. 
	 */
	
	/**
	 * 查询:
	 * 1. 查询父类记录, 做一个左外连接查询
	 * 2. 对于子类记录, 做一个内连接查询. 
	 */
	@Test
	public void testSave(){
		Person person = new Person();
		person.setName("aa");
		session.save(person);
		
		Student student = new Student();
		student.setSchool("jianqiao");
		session.save(student);
	}
	
	/**
	 * 插入操作: 
	 * 1. 对于子类对象至少需要插入到两张数据表中. 
	 */
	@Test
	public void testQuery(){
		List<Person> list = session.createQuery("from Person").list();
		System.out.println(list);
		
		List<Student> list2 = session.createQuery("from Student").list();
		System.out.println(list2);
	}
}

四.采用 union-subclass 元素的继承映射

1.Person,Student代码与上面相同

2.Person.hbm.xml

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated 2016-11-1 15:08:12 by Hibernate Tools 3.4.0.CR1 -->
<hibernate-mapping auto-import="false">
    <class name="cn.edu.sdut.hibernate.union_subclass.Person" table="PERSONS" >
        <id name="id" type="int">
            <column name="ID" />
        <span style="color:#ff0000;">    <generator class="hilo" /><!-- 禁止使用identity策略 --></span>
        </id>
        
       <property name="name" type="java.lang.String">
            <column name="NAME" />
        </property>
        
       <span style="color:#ff0000;">  <!-- 映射子类 Student, 使用 joined-subclass 进行映射 -->
         <union-subclass name="cn.edu.sdut.hibernate.union_subclass.Student" table="STUDENT">
         	<property name="school" type="string" column="SCHLLO"></property>
         </union-subclass></span>
       
    </class>
</hibernate-mapping>
3.TestSubClass

package cn.edu.sdut.hibernate.union_subclass;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import sun.reflect.generics.tree.VoidDescriptor;



public class TestSubClass {

	private SessionFactory sessionFactory;
	private Session session;
	private Transaction transaction;
	
	@Before
	public void init(){
		Configuration configuration = new Configuration().configure();
		ServiceRegistry serviceRegistry = 
				new ServiceRegistryBuilder().applySettings(configuration.getProperties())
											.buildServiceRegistry();
		sessionFactory = configuration.buildSessionFactory(serviceRegistry);
		session = sessionFactory.openSession();
		transaction = session.beginTransaction();
	}
	@After
	public void destory(){
		transaction.commit();
		session.close();
		sessionFactory.close();
	}
	
	/**
	 * 优点:
	 * 1. 无需使用辨别者列.
	 * 2. 子类独有的字段能添加非空约束.
	 * 
	 * 缺点:
	 * 1. 存在冗余的字段
	 * 2. 若更新父表的字段, 则更新的效率较低
	 */
	
	/**
	 * 查询:
	 * 1. 查询父类记录, 需把父表和子表记录汇总到一起再做查询. 性能稍差. 
	 * 2. 对于子类记录, 也只需要查询一张数据表
	 */
	@Test
	public void testSave(){
		Person person = new Person();
		person.setName("aa");
		session.save(person);
		
		Student student = new Student();
		student.setSchool("jianqiao");
		session.save(student);
	}
	
	/**
	 * 插入操作: 
	 * 1. 对于子类对象至少需要插入到一张数据表中. 
	 */
	@Test
	public void testQuery(){
		List<Person> list = session.createQuery("from Person").list();
		System.out.println(list);
		
		List<Student> list2 = session.createQuery("from Student").list();
		System.out.println(list2);
	}
	
}






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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值