title: JPA native query result returns duplicate child objects
body: <p>I have a parent table and a child Table in my DB, and have a OneToMany mapping for them in their corresponding entity classes. The child table has a foreign key parent_id. I am using JPA 2 with Hibernate and MySQL DB.</p>
  
  <p>I wish to retrieve all the parent objects and their corresponding child objects based on some parent property with an SQL Native Query.</p>
  
  <p>For that I have an SqlResultSetMapping as follows:</p>
  
  <pre><code>@SqlResultSetMapping(name="ParentsWithChildren",
         entities={ @EntityResult(entityClass = Parent.class),
                    @EntityResult(entityClass = Child.class)})
  </code></pre>
  
  <p>I am querying as follows:</p>
  
  <pre><code>String queryString = "select p.*, c.* from parent p left join child c on p.id = c.parent_id where p.property = &lt;some_property&gt;";
  Query query = entityManager.createNativeQuery(queryString, "ParentsWithChildren");
  List&lt;Object[]&gt; resultList =  query.getResultList();
  </code></pre>
  
  <p>On traversing through the result list, I find duplicate child objects for different rows in my child table as shown in the output:</p>
  
  <pre><code>for(Object obj[]: resultList){
        Parent parent = (Parent) obj[0];
        Child child = (Child) obj[1];
        System.out.println("Parent: " + parent + ", Child: " + child);
  }
  </code></pre>
  
  <p>Output:</p>
  
  <pre><code>Parent: Parent@3966c600, Child: Child@1
  Parent: Parent@3966c600, Child: Child@1
  Parent: Parent@3966c600, Child: Child@1
  Parent: Parent@3966c600, Child: Child@1
  Parent: Parent@3966c600, Child: Child@1
  Parent: Parent@3966c600, Child: Child@1
  Parent: Parent@3966c600, Child: Child@1
  Parent: Parent@3966c600, Child: Child@1
  </code></pre>
  
  <p>I don't understand why is this. Is there any way (mapping) to get all the (different) child objects with a native query.
  Fetching with column names could work and wouldn't require a corresponding object mapping, but I want to fetch all the columns of the child table and hence would prefer to user c.* in the sql query. </p>