1

I have the following entities:

@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{}

The problem that hibernate does not use the discriminator value in the queries.

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 

Shouldn't hibernate use the discriminator value in the "left jouter join" section? Something like:

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

I'm guessing this would bring a boost in performance.

Thanks, Tekin.

Tekin Omer
  • 63
  • 1
  • 10

1 Answers1

1

The discriminator value is not necessary to fetch the required data when using JOINED inheritance strategy and left joins. The JPA specification doesn't really enforce use of the discriminator in such case - explained in the answer to the Hibernate 4: persisting InheritanceType.JOINED discriminator column values.

That said, Hibernate team seems to be pretty proud they don't need to use the discriminator value as can be observed from comments HHH-6911.

Community
  • 1
  • 1
Wabi
  • 96
  • 6
  • I have read that article, but I was hoping someone would know how I could make hibernate use the discriminator for some extra performance :). Thank you for your answer. – Tekin Omer Apr 02 '15 at 06:50
  • I see. Try using @DiscriminatorOptions(force=true). It doesn't work on HIbernate 3.x for me, and I don't have a project with HIbernate 4 at hand, but might be worth the try. – Wabi Apr 03 '15 at 07:49