我使用 JPA 2 和 Hibernate 4.2.0-Final 作为提供者,我有以下实体:
@Entity
@Inheritance(strategy=InheritanceType.JOINED)
public class Person {
@Id
private String id;
.. Person attributes ..
.. Getters/Setters ..
}
@Entity
@Table(uniqueConstraints={@UniqueConstraint(name="UniqueCode", columnNames="code")})
public class Customer extends Person {
@Column(nullable=false)
private String code;
.. Other Customer attributes ..
.. Getters/Setters ..
}
我有以下 JPQL:
SELECT count(distinct c.code) FROM Customer c
Hibernate 为其生成以下 SQL:
select
count(distinct customer0_.code) as col_0_0_
from
Customer customer0_
inner join
Person customer0_1_
on customer0_.id=customer0_1_.id
但我只需要计算具有不同代码的客户,这恰好是客户特定的字段,因此不需要内部连接到“人员”。我希望 Hibernate 生成如下 SQL(即不连接表“Person”):
select
count(distinct customer0_.code) as col_0_0_
from
Customer customer0_
有没有办法告诉 Hibernate 避免不必要的内部连接?也许是一些特定于 Hibernate 的查询提示?