title: Spring Data JPA + QueryDSL Query optimization
body: <p>I have come across a weird behaviour when using QueryDSL integrated with Spring Data JPA:</p>
  
  <p>I have a ManyToOne relation between Project and Person. If I get all the projects belonging to a user by owner id (foreign key) everything works as expected:</p>
  
  <pre><code>QProject project = QProject.project;
  QPerson owner = project.owner;
  List&lt;Project&gt; projects = from(project).leftJoin(owner).fetch()
      .where(owner.id.eq(id)).list(project);
  </code></pre>
  
  <p>Generated query:</p>
  
  <pre><code>select
      project0_.id as id1_1_0_,
      person1_.id as id1_0_1_,
      project0_.creation_date as creation2_1_0_,
      project0_.name as name3_1_0_,
      project0_.owner as owner4_1_0_,
      person1_.name as name2_0_1_
  from
      project project0_
  left outer join
      person person1_
          on project0_.owner=person1_.id
  where
      project0_.owner=?
  </code></pre>
  
  <p>However, let's say we want to get all the projects belonging to a person by a field that isn't the foreign key (for instance the owner's name):</p>
  
  <pre><code>QProject project = QProject.project;
  QPerson owner = project.owner; 
  List&lt;Project&gt; projects = from(project).leftJoin(owner).fetch()
      .where(owner.name.eq(name)).list(project);
  </code></pre>
  
  <p>In these cases, the table Person is joined twice unnecessarily (notice person1_ and person2_):</p>
  
  <pre><code>select
      project0_.id as id1_1_0_,
      person1_.id as id1_0_1_,
      project0_.creation_date as creation2_1_0_,
      project0_.name as name3_1_0_,
      project0_.owner as owner4_1_0_,
      person1_.name as name2_0_1_
  from
      project project0_
  left outer join
      person person1_
          on project0_.owner=person1_.id cross
  join
      person person2_
  where
      project0_.owner=person2_.id
      and person2_.name=?
  </code></pre>
  
  <p>Any idea why this is happening and how to avoid it?</p>