title: How do I perform an outer join with a JPA CriteriaBuilder query?
body: <p>I'm using JPA 2.0, Hibernate 4.1.0.Final, and MySQL 5.5.27.  I want to construct a JPA query that will return two entities per row and I want to perform a right outer join.  The two entities are:</p>
  
  <pre><code>@Entity
  @Table(name = "user",
      uniqueConstraints = { @UniqueConstraint(columnNames = { "USER_NAME" }) }
  )
  public class User implements Comparable&lt;User&gt;, Serializable
  {
      ...
      @Column(name = "first_name")
      @NotNull
      /* the first name of the User */
      private String firstName;
  </code></pre>
  
  <p>and</p>
  
  <pre><code>@Entity
  @Table(name="code_user",
      uniqueConstraints = { 
          @UniqueConstraint(columnNames = { "CODE_ID", "USER_ID" }) }
  )
  public class CodeUser 
  {
  
      @Id
      @NotNull
      @GeneratedValue(generator = "uuid-strategy")
      @Column(name = "ID")
      private String id;
  
      @ManyToOne
      @JoinColumn(name = "CODE_ID", nullable = false, updatable = true)
      private Code code;
  
      @ManyToOne
      @JoinColumn(name = "USER_ID", nullable = false, updatable = true)
      private User user;
  </code></pre>
  
  <p>Here's my JPA so far, but the problem is that an inner join is being performed, and only one entity per row (the User object) is returned ...</p>
  
  <pre><code>    final CriteriaBuilder builder = m_entityManager.getCriteriaBuilder();
      final CriteriaQuery&lt;User&gt; criteria = builder.createQuery(User.class);
      final Root&lt;User&gt; user = criteria.from(User.class);
  
      final Root&lt;CodeUser&gt; codeUser = criteria.from(CodeUser.class);
      final Join&lt;CodeUser, User&gt; joinUser = codeUser.join(CodeUser_.user);
      criteria.select(joinUser);
      …
      return criteria.where(builder.and(preds.toArray(new Predicate[preds.size()])));
  </code></pre>
  
  <p>How do I perform an outer join so that all Users are returned and any matching CodeUser records are also returned?  It's not an option to add extra member fields to my User entity.</p>