title: How do I use CriteriaBuilder to write a left outer join when relationship goes the other way?
body: <p>I’m using JPA 2.0, Hibernate 4.1.0.Final and MySQL 5.5.37.  I have the following two entities …</p>
  
  <pre><code>@Entity
  @Table(name = "msg")
  public class Message
  {
  
      @Id
      @NotNull
      @GeneratedValue(generator = "uuid-strategy")
      @Column(name = "ID")
      private String id;
  
      @Column(name = "MESSAGE", columnDefinition="LONGTEXT")
      private String message;
  </code></pre>
  
  <p>and</p>
  
  <pre><code>@Entity
  @Table(name = "msg_read", uniqueConstraints = { @UniqueConstraint(columnNames = { "MESSAGE_ID", "RECIPIENT" }) })
  public class MessageReadDate
  {
  
      @Id
      @NotNull
      @GeneratedValue(generator = "uuid-strategy")
      @Column(name = "ID")
      private String id;
  
      @ManyToOne
      @JoinColumn(name = "RECIPIENT", nullable = false, updatable = true)
      private User recipient;
  
      @ManyToOne
      @JoinColumn(name = "MESSAGE_ID", nullable = false, updatable = true)
      private Message message;
  
      @Column(name = "READ_DATE")
      private java.util.Date readDate;
  </code></pre>
  
  <p>Using CriteriaBuilder, how would I write this</p>
  
  <pre><code>SELECT DISTINCT m.*
  FROM msg AS m
  LEFT JOIN msg_read AS mr
          ON mr.message_id = m.id AND mr.recipient = 'USER1'
  </code></pre>
  
  <p>?  My problem is that there is no field “msg_read” in my Message entity, and I’m not sure how to specify the “AND” part of the left outer join in CriteriaBuilder.</p>