I'm using JPA 2 with Hibernate 4.2.0-Final as provider and I have the following entities:
Code:
@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 ..
}
And I have the following JPQL:
Code:
SELECT count(distinct c.code) FROM Customer c
For which Hibernate is generating the following SQL:
Code:
select
count(distinct customer0_.code) as col_0_0_
from
Customer customer0_
inner join
Person customer0_1_
on customer0_.id=customer0_1_.id
But I only need to
count Customers with distinct code, which happens to be a field that is specific to Customer. I don't need any field/attribute stored on subclass or parent class, so there is no need for the inner join to 'Person'. I'd like Hibernate to generate the SQL like follows (i.e. without joinning table 'Person'):
select
count(distinct customer0_.code) as col_0_0_
from
Customer customer0_
Is there a way to tell Hibernate to avoid the unnecessary inner join?
Thanks,
Rafael.