Suppose I have an JPA entity and a query:
@Entity
public class MyEntity {
@OneToMany(fetch = FetchType.LAZY)
private List<ChildEntity> children = new ArrayList<ChildEntity>();
}
public List<MyEntity> fetchAll() {
return em.createQuery("select distinct e from MyEntity e join fetch e.children")
.getResultList();
}
Without distinct keyword it will do a cross-product of MyEntity and e.children.
Is it considered a good practice to use both distinct and join fetch to avoid N+1 Select problem with collections? Does it have side-effects?
You must use “SELECT DISTINCT” because is being executed a cartesian product between MyEntity and children.
Sorry for my English