title: EclipseLink ManyToOne - CriteriaBuilder Generated Query is Wrong
body: <p>I have an Entity with a ManyToOne Relationship to the Primary Key of another entity. When I create a query that references this Foreign Key eclipseLink always creates a join instead of simply accessing the Foreign Key.</p>
  
  <p>I have created a highly simplified example to show my issue:</p>
  
  <pre><code>@Entity
  public class House {
    @Id
    @Column(name = "H_ID")
    private long id;
  
    @Column(name = "NAME")
    private String name;
  
    @ManyToOne
    @JoinColumn(name = "G_ID")
    private Garage garage;
  }
  
  @Entity
  public class Garage{
    @Id
    @Column(name = "G_ID")
    private long id;
  
    @Column(name = "SPACE")
    private Integer space;
  }
  </code></pre>
  
  <p>I created a query that should return all houses that either have no garage or have a garage with G_ID = 0 using the CriteriaBuilder.</p>
  
  <pre><code>CriteriaBuilder cb = entityManager.getCriteriaBuilder();
  CriteriaQuery&lt;House&gt; query = cb.createQuery(House.class);
  Root&lt;House&gt; houseRoot = query.from(House.class);
  Path&lt;Long&gt; garageId = houseRoot.get(House_.garage).get(Garage_.id);
  query.where(cb.or(cb.equal(garageId , 0), cb.isNull(garageId)));
  TypedQuery&lt;House&gt; typedQuery = entityManager.createQuery(query);
  List&lt;House&gt; houses = typedQuery.getResultList();
  </code></pre>
  
  <p>The generated query is:</p>
  
  <pre><code>SELECT h.NAME, h.G_ID FROM HOUSE h, GARAGE g WHERE (((h.G_ID= 0) OR (g.G_ID IS NULL)) AND (g.G_ID = h.G_ID));
  </code></pre>
  
  <p>I don't understand why</p>
  
  <ul>
  <li>The or condition first references table HOUSE and then GARAGE (instead of HOUSE)</li>
  <li>The join is created in the first place.</li>
  </ul>
  
  <p>The correct query should look like this in my understanding:</p>
  
  <pre><code>SELECT h.NAME, h.G_ID FROM HOUSE h WHERE (((h.G_ID= 0) OR (h.G_ID IS NULL));
  </code></pre>
  
  <p>Or if a join is made it should take into account that the ManyToOne relationship is nullable and therefore do a LEFT OUTER JOIN.</p>
  
  <pre><code>SELECT h.NAME, h.G_ID FROM HOUSE h LEFT OUTER JOIN GARAGE g ON (h.G_ID = g.G_ID ) WHERE (h.G_ID = 0) OR (g.G_ID IS NULL);
  </code></pre>
  
  <p>(Note both these queries would work correctly in my more complicated setup. I also get the same error when only wanting to retrieve all houses that have no garage.)</p>
  
  <p>How can I achieve this (while still using the CriteriaBuilder and ideally not having to change the DB Model)?</p>
  
  <p>(Please let me know any additional information that might be required, I'm very new to this topic and came across this issue while migrating an existing application.)</p>
  
  <p>-- edit --
  I have found a solution to my problem that will result in slightly different behaviour (but in my application that part of the code I had to migrate didn't make much sense in the first place). Instead of using</p>
  
  <pre><code>Path&lt;Long&gt; garageId = houseRoot.get(House_.garage).get(Garage_.id);
  </code></pre>
  
  <p>I use</p>
  
  <pre><code>Path&lt;Garage&gt; garage = houseRoot.get(House_.garage);
  </code></pre>
  
  <p>And then as expected table Garage isn't joined anymore. (I assume the code previously must have been some kind of hack to get the desired behaviour from openJPA)</p>