hibernate - Avoiding outer joins across tables when using joined inheritance with Spring Data JPA -
consider following classes in spring data jpa (+ hibernate) application:
@entity @inheritance(strategy = inheritancetype.joined) @table(name = "person") public class person { } @entity @table(name = "customer") public class customer extends person { } @entity @table(name = "employee") public class employee extends person { } @entity @table(name = "manager") public class manager extends employee { } public interface ipersonrepository extends jparepository<person, long> { } public interface icustomerrepository extends jparepository<customer, long> { } public interface iemployeerepository extends jparepository<employee, long> { } my common use case involves calling following method (inherited jparepository):
ipersonrepository.findall(); whenever method invoked, following sql query issued hibernate:
select person0_.id id1_3_, person0_.version version2_3_, person0_.first_name first3_3_, person0_.last_name last4_3_, person0_1_.customer_code customer1_0_, person0_2_.employee_code employee1_1_, person0_2_.manager_id manager3_1_, case when person0_3_.id not null 3 when person0_1_.id not null 1 when person0_2_.id not null 2 when person0_.id not null 0 end clazz_ person person0_ left outer join customer person0_1_ on person0_.id=person0_1_.id left outer join employee person0_2_ on person0_.id=person0_2_.id left outer join manager person0_3_ on person0_.id=person0_3_.id; whenever query executed, interested in common fields in person class, find left outer joins useless.
the problem in our actual application, there 8 child classes employee , customer , millions of records in each child table, causing query on parent table run slow.
is there way avoid outer joins across tables in case? please note have tried using discriminatorcolumn approach , joins still performed in case (when using hibernate). have tried hibernate-specific polymorphism annotation on entity classes in possible combinations , still outer joins performed.
spring data jpa version: 1.2.0 hibernate version: 4.2.1
after many days of trying solve problem, have come following conclusion:
- there no way force hibernate 4.x (and 3.x) not perform outer joins in case.
- there no way force latest available versions of toplink essentials (v2.1-60) , openjpa (v2.2.2) not perform outer joins either.
- it possible avoid outer joins latest available version of eclipselink (v2.5.0). however, eclipselink requires discriminator column class hierarchy shown above, though hibernate , openjpa not. far have been unable find way avoid using discriminator column eclipselink.
i guess have wait either jpa specification change or jpa implementation become available satisfies current requirement.
Comments
Post a Comment