title: JPA EntityGraph: Hibernate attempts to fetch multiple collections in a single query leading to large a Cartesian product
body: <p>I am using Hibernate behind JPA. I am using EntityGraph to fetch the required data.</p>
  
  <p>I have classes A, B and C</p>
  
  <pre><code>class A {
    Integer attrib1;
    String attrib2;
    List&lt;B&gt; blist; // typical size: about 250 objects
    List&lt;C&gt; clist; // typical size about 100 objects
  }
  </code></pre>
  
  <p>Classes B and C have their own subgraphs that I am fetching along with the main graph.</p>
  
  <p>The problem is occurring with the main graph.</p>
  
  <p>It is generating 3 SQL queries in all to fetch all the required data.
  The problem is with the first query. </p>
  
  <p>In an attempt to reduce the number of queries (that's what I think), in the first query, Hibernate does a join on A, B and C.</p>
  
  <p>i.e </p>
  
  <p>A 
  left outer join B on A.x=B.x
  left outer join C on A.y=C.y</p>
  
  <p>This creates highly sub-optimal result set with several tens of thousands of rows with a lot a duplicated data across rows.</p>
  
  <p>I can see that the same query can turn out to be very effective by avoiding separate queries for each collection if the number of relations between A and B &amp; A and C is small. However, in this case, since the number of relations is large, it fails miserably.</p>
  
  <p>How do I make hibernate not do this?</p>
  
  <p>I would like Hibernate to generate a separate SQL query for each Collection. This way the cost would go up linearly with collection size. </p>