title: What is the standard way to avoid both N+1 and the Cartesian Product issues while fetching collections with JPA
body: <p>When one has an entity with fields part of which are collections, one wishes to fetch the data with the smallest number of queries possible and using as less memory as possible. </p>
  
  <p>The first problem is addressed by "join fetch" in JPQL queries (solving the N+1 problem). </p>
  
  <p>However "join fetch" (as easily seen by inspecting the respective SQL query) causes the Cartesian product problem: each "row" which corresponds to the entity fields without multiplicities, is present in the returned result set with multiplicity N_1 x N_2 x ... x N_m, where N_1 is the multiplicity of the first collection, N_2 is the multiplicity of the second, and N_m is the multiplicity of the m-th collection, assuming that the entity has m fields which are collections.</p>
  
  <p>Hibernate solves this problem with FetchMode.SUBSELECT (which, If I am not mistaken, makes m+1 queries, each of which returns no redundant data). What is the <b>standard</b> way to resolve this issue in JPA (it seems to me I cannot mix, at least in this case, JPA annotations with those of Hibernate)? </p>