hibernate继承映射

一.

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

hibernate继承映射_第1张图片

注意区别:

hibernate继承映射_第2张图片

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

hibernate继承映射_第3张图片

代码实现:

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





    discriminator-value="person">
        
            
            
        
        
        
        
        
        
            
        
        
        
        
        	
        
    

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();
	}
	
	/**
	 * 缺点:
	 * 1. 使用了辨别者列.
	 * 2. 子类独有的字段不能添加非空约束.
	 * 3. 若继承层次较深, 则数据表的字段也会较多. 
	 */
	
	/**
	 * 插入操作: 
	 * 1. 对于子类对象只需把记录插入到一张数据表中.
	 * 2. 辨别者列有 Hibernate 自动维护. 
	 */
	@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. 查询父类记录, 只需要查询一张数据表
	 * 2. 对于子类记录, 也只需要查询一张数据表
	 */
	@Test
	public void testQuery(){
		//Person是类
		List list = session.createQuery("from Person").list();
		System.out.println(list);
		
		List list2 = session.createQuery("from Student").list();
		System.out.println(list2);
	}
}

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

hibernate继承映射_第4张图片

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





    
        
            
            
        
        
       
            
        
        
         
        
        	
        	
        	
        
    


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 list = session.createQuery("from Person").list();
		System.out.println(list);
		
		List list2 = session.createQuery("from Student").list();
		System.out.println(list2);
	}
}

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

hibernate继承映射_第5张图片

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

2.Person.hbm.xml





    
        
            
            
        
        
       
            
        
        
         
         
         	
         
       
    

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 list = session.createQuery("from Person").list();
		System.out.println(list);
		
		List list2 = session.createQuery("from Student").list();
		System.out.println(list2);
	}
	
}






你可能感兴趣的:(Hibernate)