title: How to retrieve parent entities with a collection of child entities using hibernate jpa native query?
body: <p>I normally use JPQL to query database, but today I have to use native query for performance reason and for features not available in JPQL (join subquery). Using JPQL one can use join fetch to retrieve parent entities along with their collections of child entities. However when I try to use native query, for each parent entity, multiple rows are returned, each row contains the data of the parent and one of its children. The returned List then contains duplicated objects for the parent. Suppose one parent have two children, then the following query returns 2 parents in the list instead of 1.</p>
  <pre><code>@Query(value=&quot;select * from Parent p left join Child c on p.parent_id = c.parent_id&quot;, nativeQuery = true)
  List&lt;Parent&gt; getParents()
  </code></pre>
  <p>Anyone knows how to avoid the duplicated parents so it returns same result as below</p>
  <pre><code>@Query(value=&quot;select p from Parent p left join fetch p.children&quot;)
  List&lt;Parent&gt; getParents()
  </code></pre>