Hibernate在查询中不使用InheratorType.JOINED

时间:2015-04-01 15:20:41

标签: hibernate inheritance discriminator

我有以下实体:

@Entity
@Table(name = "employee")
@Inheritance(strategy = InheritanceType.JOINED)
@DiscriminatorColumn(name="TYPE") 
public class Employee {}

@Entity
@Table(name = "regular_employee")
@PrimaryKeyJoinColumn(name = "id")
@DiscriminatorValue("R")
public class RegularEmployee extends Employee{}

hibernate在查询中不使用鉴别​​器值的问题。

select
    employee0_.id as id1_1_,
    employee0_.name as name2_1_,
    employee0_.type as type3_1_,
    employee0_1_.pay_per_hour as pay_per_1_0_,
    case 
        when employee0_1_.id is not null then 1 
        when employee0_.id is not null then 0 
    end as clazz_ 
from
    employee employee0_ 
left outer join
    regular_employee employee0_1_ 
        on employee0_.id=employee0_1_.id 

不应该休眠使用“左侧jouter连接”部分中的鉴别器值?类似的东西:

left outer join
    regular_employee employee0_1_ 
        on employee0_.type='R' and employee0_.id=employee0_1_.id

我猜这会带来性能提升。

谢谢, 特勤。

1 个答案:

答案 0 :(得分:1)

使用JOINED继承策略和左连接时,不需要使用鉴别器值来获取所需的数据。在这种情况下,JPA规范并没有真正强制使用鉴别器 - 在Hibernate 4: persisting InheritanceType.JOINED discriminator column values的答案中有解释。

也就是说,Hibernate团队似乎非常自豪他们不需要使用鉴别器值,这可以从评论HHH-6911中看出。