When Entity has more than one @ManyToOne (unidirectional) relations to some other entity,
HQL query with left join fetch makes N+1 selects. This does not happen without multiple @ManyToOne relations to the same entity.
Example:
Code:
@Entity
public class Country {
@Id
private String name;
}
@Entity
public class Person {
@Id
private Integer personId;
@ManyToOne
private Country nationality;
@ManyToOne
private Country taxDomicile;
}
Now, this HQL:
Code:
session.createQuery("from Person p left join fetch p.nationality left join fetch p.taxDomicile")
makes N+1 selects - main select from Person entity and then selects from Country for each row in Person.
When there is only one relation to Country entity (e.g. delete taxDomicile field) join fetch works correctly (only one select joining Person and Country).
Is this behaviour known? Is it a feature, Is it a bug?
Tested on 4.3.1.Final