上一节《继承结构中每个子类单独一张表》中,我们讲了在继承结构中每个子类一张单独的表,父类没有表的情况,今天我们讲一下,继承结构中,父类一张表,存放公共的数据,子类各一张表,存放子类各自独特的特性数据,就是说每个类都有一张表。
新建一个java项目,结构如下:
实体类和上一节一样《继承结构中每个子类单独一张表》,
不同的是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="com.robert.pojo">
<!-- abstract="true":指明Person类是抽象的,不生成对应的person数据库表 -->
<class name="Person" abstract="true">
<id name="id" column="id">
<!-- assigned:自己指定主键 -->
<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>
这个配置可以参考文档,如图:
使用Junit4运行testCreateDB,生成对应的数据库表,sql语句如:
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)
数据库表如图:
执行testSave()方法,代码如下:
/**
* 保存数据
*
* @throws HibernateException
* @throws SerialException
* @throws SQLException
* @throws IOException
*/
@Test
public void testSave() throws HibernateException, SerialException,
SQLException, IOException {
Session session = null;
Transaction tx = null;
try {
session = HibernateUtil.getSession();
tx = session.beginTransaction();
Teacher t1 = new Teacher() ;
t1.setId(1) ;
t1.setAge(32) ;
t1.setName("张老师") ;
t1.setSalary(5000) ;
Student stu1 = new Student() ;
stu1.setId(2) ;
stu1.setAge(22) ;
stu1.setName("王同学") ;
stu1.setWork("家庭作业1") ;
Student stu2 = new Student() ;
stu2.setId(3) ;
stu2.setAge(24) ;
stu2.setName("刘同学") ;
stu2.setWork("housework") ;
session.save(t1) ;
session.save(stu1) ;
session.save(stu2) ;
tx.commit();
} catch (HibernateException e) {
if (tx != null) {
tx.rollback();
}
e.printStackTrace();
throw e;
} finally {
HibernateUtil.closeSession();
}
}
生成的sql语句如:
Hibernate:
insert
into
Person
(name, age, id)
values
(?, ?, ?)
Hibernate:
insert
into
Teacher
(salary, 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
Student
(work, id)
values
(?, ?)
数据库数据如图:
总结:
加上前两节关于继承结构的讲解,继承结构的三种继承映射就讲完了,今天做一个比较:
1、所有数据存一张表:
缺点是数据冗余多,优点是效率高。-------推荐使用;
2、每个子类一张单独表,没有父类表:
优点是数据冗余少,缺点是查询效率低,主键不能自增,需要指明为assigned、uuid、Hilo等。
3、每个类一张表:
优点是数据冗余较少,缺点是:效率比较每个子类张表稍高。维护表的个数多。