hibernate繼承映射策略之每個子類一張表

以文章《hibernate單表繼承映射》中的幾個類爲例,我們來看下“每個子類一張表”的映射策略。

與之不同的是Person.hbm.xml配置文件,代碼如下:

<hibernate-mapping package="com.test.pojo">
	<class name="Person">
		<id name="id">
			<!-- assigned:自己指定主鍵 -->
			<generator class="assigned"></generator>
		</id>
		<property name="name" />
		<property name="age" />
		<joined-subclass name="Student">
			<key column="id" />
			<property name="work" />
		</joined-subclass>
		<joined-subclass name="Teacher">
			<key column="id" />
			<property name="salary" />
		</joined-subclass>
	</class>
</hibernate-mapping>
測試類:

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 save(){
		Session session=null;
		Transaction tx=null;
		try{
			session=HibernateUtil.getSession();
			tx=session.beginTransaction();
			Teacher teacher=new Teacher();
			teacher.setId(1);
			teacher.setName("teacher");
			teacher.setAge(20);
			teacher.setSalary(5000);
			Student student=new Student();
			student.setId(2);
			student.setName("stu1");
			student.setAge(20);
			student.setWork("hello world");
			Student stu=new Student();
			stu.setId(3);
			stu.setName("stu2");
			stu.setAge(21);
			stu.setWork("Struts2");
			session.save(student);
			session.save(stu);
			session.save(teacher);
			tx.commit();
		}catch(Exception e){
			if(tx!=null)
				tx.rollback();
			e.printStackTrace();
		}finally{
			HibernateUtil.closeSession();
		}
	}
}
執行testCreateDB,打印信息如下:

alter table Student 
        drop 
        foreign key FK_ohs43dct8k52ch2exlmf4bs3l

alter table Teacher 
        drop 
        foreign key FK_g6jmt7fcm6gfd0jvhimb9xy84

drop table if exists Person

    drop table if exists Student

    drop table if exists Teacher

    create table Person (
        id integer not null,
        name varchar(255),
        age integer,
        primary key (id)
    )

    create table Student (
        id integer not null,
        work varchar(255),
        primary key (id)
    )

    create table Teacher (
        id integer not null,
        salary integer,
        primary key (id)
    )

    alter table Student 
        add constraint FK_ohs43dct8k52ch2exlmf4bs3l 
        foreign key (id) 
        references Person (id)

    alter table Teacher 
        add constraint FK_g6jmt7fcm6gfd0jvhimb9xy84 
        foreign key (id) 
        references Person (id)

數據庫表信息如下:


執行save方法,打印sql語句如下:

Hibernate: 
    insert 
    into
        Person
        (name, age, id) 
    values
        (?, ?, ?)
Hibernate: 
    insert 
    into
        Student
        (work, id) 
    values
        (?, ?)
Hibernate: 
    insert 
    into
        Person
        (name, age, id) 
    values
        (?, ?, ?)
Hibernate: 
    insert 
    into
        Student
        (work, id) 
    values
        (?, ?)
Hibernate: 
    insert 
    into
        Person
        (name, age, id) 
    values
        (?, ?, ?)
Hibernate: 
    insert 
    into
        Teacher
        (salary, id) 
    values
        (?, ?)

數據庫表信息如下:


總結:

三種繼承映射比較:

1、所有數據存一張表,數據冗餘比較多。效率比較高。---推薦使用

2、每個子類一張表,數據冗餘比較少,查詢效率低,主鍵不能自增。需要指明爲assigned,uuid,hilo等。

3、每個類一張表,數據冗餘比較少。查詢效率比每個子類一張表稍高。維護的表個數多。







發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章