Hibernate类的继承使用joined-class实现
类与表的关系:
*************
Employee.java
*************
sale
skill
Hibernate生成的sql语句为:
Hibernate: insert into employees (EMPLOYEE_NAME) values (?)
Hibernate: insert into sale (SELL, SALE_ID) values (?, ?)
Hibernate: insert into employees (EMPLOYEE_NAME) values (?)
Hibernate: insert into SKILL (SKILLER, SKILL_ID) values (?, ?)
Hibernate: insert into employees (EMPLOYEE_NAME) values (?)Hibernate:
select
sale0_.SALE_ID as EMPLOYEE1_0_0_,
sale0_1_.EMPLOYEE_NAME as EMPLOYEE2_0_0_,
sale0_.SELL as SELL1_0_
from sale sale0_
inner join
employees sale0_1_
on
sale0_.SALE_ID=sale0_1_.EMPLOYEE_ID
where
sale0_.SALE_ID=?
Hibernate:
select
skill0_.SKILL_ID as EMPLOYEE1_0_0_,
skill0_1_.EMPLOYEE_NAME as EMPLOYEE2_0_0_,
skill0_.SKILLER as SKILLER2_0_
from SKILL skill0_
inner join
employees skill0_1_
on
skill0_.SKILL_ID=skill0_1_.EMPLOYEE_ID
where
skill0_.SKILL_ID=?测试结果为:
Sale{id=1, name=sunliusell=300000}
Skill{id=2, name=wangwuskiller=java}
Employee{id=3, name=lisi}
Hibernate:
select
employee0_.EMPLOYEE_ID as EMPLOYEE1_0_0_,
employee0_.EMPLOYEE_NAME as EMPLOYEE2_0_0_,
employee0_1_.SELL as SELL1_0_,
employee0_2_.SKILLER as SKILLER2_0_,
case
when employee0_1_.SALE_ID is not null then 1
when employee0_2_.SKILL_ID is not null then 2
when employee0_.EMPLOYEE_ID is not null then 0
end
as clazz_0_
from
employees employee0_
left outer join
sale employee0_1_
on
employee0_.EMPLOYEE_ID=employee0_1_.SALE_ID
left outer join
SKILL employee0_2_
on
employee0_.EMPLOYEE_ID=employee0_2_.SKILL_ID
where
employee0_.EMPLOYEE_ID=?
PS:
采用这种方式的好处是数据库的表结构符合关系模型的设计理念,子类对应的字段可以强制为非空,如果要新增一个子类,只需要新增一个表即可;缺点是效率不高,而且表的个数比较多。