考虑 Spring Data JPA (+ Hibernate) 应用程序中的以下类:
@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> { }
我最常见的用例涉及调用以下方法(继承自JpaRepository
):
IPersonRepository.findAll();
每当调用此方法时,Hibernate 都会发出以下 SQL 查询:
select
person0_.id as id1_3_,
person0_.version as version2_3_,
person0_.first_name as first3_3_,
person0_.last_name as last4_3_,
person0_1_.customer_code as customer1_0_,
person0_2_.employee_code as employee1_1_,
person0_2_.manager_id as manager3_1_,
case
when person0_3_.id is not null then 3
when person0_1_.id is not null then 1
when person0_2_.id is not null then 2
when person0_.id is not null then 0
end as clazz_
from
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;
每当执行这个查询时,我只对Person
类中的公共字段感兴趣,所以我发现左外连接没用。
问题是在我们的实际应用中,每个子表有8个子类Employee
和数Customer
百万条记录,导致对父表的查询运行非常慢。
在这种情况下,有没有办法避免跨表的外部连接?请注意,我已尝试使用该DiscriminatorColumn
方法,并且在这种情况下仍会执行连接(使用 Hibernate 时)。我还在Polymorphism
所有可能的组合中尝试了实体类上的特定于 Hibernate 的注释,并且仍然执行了外部连接。
Spring Data JPA version: 1.2.0
Hibernate version: 4.2.1