0

My Problem is as follows

@Entity
public class MainType{
    @Fetch(FetchMode.JOIN)
    @ManyToOne(fetch = FetchType.EAGER)
    private SubType subObject;
}

@Entity
public class SubType{
    @Fetch(FetchMode.JOIN)
    @ManyToOne(fetch = FetchType.EAGER)
    private SubSubType subsubObject;
}

When I query the MainType with Hibernate to recieve a Collection hibernate will make a join. Something like select * from MainType left join SubType which is good but then it makes another select for each record that the first query returned to load SubSubType. I would like to load everything in a single query like select * from MainType left join SubType left join SubSubType.

Do you know any way to make that happen?

Mr.H.
  • 965
  • 1
  • 10
  • 18

1 Answers1

0

In HQL/JPQL you'd write that almost as you mentioned:

SELECT mt FROM MainType mt 
  LEFT JOIN FETCH mt.subObject so 
  LEFT JOIN FETCH so.subSubObject sso

The key is to apply a JOIN FETCH and not just a JOIN to avoid the additional query that will fetch the objects after acquiring the identifiers.

Naros
  • 19,928
  • 3
  • 41
  • 71
  • Yes I thought about that way too. But i am Using the Criteria API for querying. And aditionally i want all the querys for MainType to look like this not only the one i am writing now. Isn't there a chance to force this way of querying inside the modeldeclaration. – Mr.H. Feb 10 '17 at 08:13