title: Hibernate - Selecting across multiple joins with collections
body: <p>I'm having trouble getting a hibernate select to return a correctly populated object graph, when the select contains joins across many collections.</p>
  
  <p>Eg:</p>
  
  <pre><code>    String sql = "select distinct changeset " +
              "from Changeset changeset " +
              "join fetch changeset.changeEntries as changeEntry " +
              "join fetch changeEntry.repositoryEntity as repositoryEntity " +
              "join fetch repositoryEntity.repository as repository " +
              "where repository.connectionName = :connectionName";
  </code></pre>
  
  <ul>
  <li>A Changeset has many ChangeEntries </li>
  <li>A ChangeEntry has one RepositoryEntity</li>
  <li>A RepositoryEntity has one Repository</li>
  </ul>
  
  <p>The above statement returns the correct data, but the object graph is not correctly populated -- ie., each Changeset contains every ChangeEntry, not just it's own children.</p>
  
  <p>Here's the relevant snippets of those two classes:</p>
  
  <pre><code>public class Changeset {
  
      @NotNull
      @OneToMany(mappedBy="changeset", targetEntity=ChangeEntry.class, cascade={CascadeType.ALL }, fetch=FetchType.EAGER )
      private Set&lt;IChangeEntry&gt; changeEntries;
  </code></pre>
  
  <p>And...</p>
  
  <pre><code>public class ChangeEntry {
  
      @NotNull
      @ManyToOne(targetEntity=Changeset.class)
      @ForeignKey(name="FkChangeEntryChangeset")
      private IScmChangeset changeset;
  
      @NotNull
      @ManyToOne(targetEntity=RepositoryEntity.class, cascade=CascadeType.ALL,fetch=FetchType.EAGER)
      @ForeignKey(name="FkChangeEntryRepoEntity")
      private IRepositoryEntity repositoryEntity;
  </code></pre>
  
  <p>Any assistance is greatly appreciated</p>
  
  <p>Regards</p>
  
  <p>Marty</p>