title: JPA N+1 iteration and JSON serializer : query are still there
body: <p><strong>THE PROBLEM</strong></p>
  
  <p>I have the entity Product with a lot of relationship: productor, category and so on.</p>
  
  <p>When I ask for the products'list, fist of all JPA do the normal query:</p>
  
  <pre><code>select t1.a, t1.b,..., t1.idcategory, ... from product t1 limit 50 offset 0
  </code></pre>
  
  <p>Then, for each product ask for category and the other relationship</p>
  
  <pre><code>select * from category c where c.id = ?
  </code></pre>
  
  <p>This is very heavy : 50 rows produce 50xrelationship queries (in my case about 8 relationship, so 50+50*8 queries).
  I need to remove these queries.</p>
  
  <p><strong>FIRST TRY</strong></p>
  
  <p>I use criteria query, so i have added the joins to the query:</p>
  
  <pre><code>Root&lt;T&gt; root  = query.from(Product.class);
  Map&lt;Class,Field&gt; foreignRelationship = EntityUtils.listEntity(Product.class);
  for(Field f : foreign.values()){
     root.join(f.getName());
  }
  </code></pre>
  
  <p>This code read my entity and produces the correct query:</p>
  
  <pre><code>select t1.* from product t1 join category c on t1.idcategory = c.id ...
  </code></pre>
  
  <p>The query is builded successfully, but when i call the json serializer (I need to return the object via a REST service), the NxROW queries are executed :(</p>
  
  <p><strong>SECOND TRY</strong>
  I'have tryed with NamedEntityGraph:</p>
  
  <pre><code>@NamedEntityGraphs({
  @NamedEntityGraph(name = "graph.Product", 
      attributeNodes = {
          @NamedAttributeNode(value = "producer",
                  subgraph = "graph.Product.producer"),
          @NamedAttributeNode(value = "catmer"),
          @NamedAttributeNode(value = "shop"),
          ...
      },
      subgraphs = {
          @NamedSubgraph(
              name = "graph.Product.producer",
              attributeNodes = {
                  @NamedAttributeNode(value = "id"),
                  @NamedAttributeNode(value = "code")
              }
          ),...
      },
      includeAllAttributes = true
  )
  })
  </code></pre>
  
  <p>In my criteria query:</p>
  
  <pre><code>getEntityManager().createQuery(query)
                  //.setHint("javax.persistence.fetchgraph",graph);
                  .setHint("javax.persistence.loadgraph",graph);
  </code></pre>
  
  <p>The query has no join and all the per-row queries are done :(</p>
  
  <p>How can I solve this problem? Are there some mistakes in my approach?</p>
  
  <p>PS: I'am using eclipselink</p>