title: How to create JPA NamedQuery with LEFT JOIN
body: <p>I have two entities: Car and Reservation. I would like to create named query with LEFT JOIN. I was trying to do this like it is described here <a href="https://stackoverflow.com/questions/10202456/how-to-create-a-jpa-query-with-left-outer-join">How to create JPA query with LEFT OUTER JOIN</a> but it doesn't work. Do you have any idea what is wrong in my query? I would like to show Cars which have NULL reservations. Anyway even with JOIN it does not work. After starting application I have an error:</p>
  
  <pre><code>Caused by: org.hibernate.HibernateException: Errors in named queries: Car.findAll
      at org.hibernate.internal.SessionFactoryImpl.&lt;init&gt;(SessionFactoryImpl.java:495) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final]
      at org.hibernate.boot.internal.SessionFactoryBuilderImpl.build(SessionFactoryBuilderImpl.java:444) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final]
      at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:879) ~[hibernate-entitymanager-5.0.12.Final.jar:5.0.12.Final]
      ... 22 common frames omitted
  </code></pre>
  
  <p>In principle I would like to achieve this query which in MySQL works</p>
  
  <pre><code>SELECT distinct * FROM car c LEFT JOIN reservation r ON c.id = r.car_id WHERE c.producer='producer' AND c.model='model' AND c.type='type' 
  AND (r.date_of_rent &lt; 'date1' AND r.date_of_return &lt; 'date1') OR (r.date_of_rent &gt; 'date2') OR r.date_of_rent IS NULL;
  </code></pre>
  
  <p>Car Entity</p>
  
  <pre><code>import java.io.Serializable;
  import java.util.List;
  
  import javax.persistence.Entity;
  import javax.persistence.GeneratedValue;
  import javax.persistence.GenerationType;
  import javax.persistence.Id;
  import javax.persistence.NamedQuery;
  import javax.persistence.OneToMany;
  
  @Entity
  @NamedQuery(name = "Car.findAll", query = "SELECT c FROM Car c LEFT JOIN c.reservation r WHERE c.producer=:producer "
          + "AND c.type=:type AND c.dailyPrice=:dailyPrice")
  public class Car implements Serializable {
      private static final long serialVersionUID = 1L;
  
      @Id
      @GeneratedValue(strategy = GenerationType.IDENTITY)
      private Long id;
      private String producer;
      private String model;
      private int seatsNumber;
      private String type;
      private String registrationNumber;
      private double dailyPrice;
      private String description;
      @OneToMany(mappedBy = "car")
      private List&lt;Reservation&gt; reservations;
  </code></pre>
  
  <p>Reservation Entity</p>
  
  <pre><code>import java.io.Serializable;
  import java.sql.Date;
  
  import javax.persistence.Entity;
  import javax.persistence.GeneratedValue;
  import javax.persistence.GenerationType;
  import javax.persistence.Id;
  import javax.persistence.ManyToOne;
  
  @Entity
  public class Reservation implements Serializable {
      private static final long serialVersionUID = 1L;
  
      @Id
      @GeneratedValue(strategy = GenerationType.IDENTITY)
      private Long id;
      @ManyToOne
      private User user;
      @ManyToOne
      private Car car;
      private Date dateOfRent;
      private Date dateOfReturn;
  </code></pre>
  
  <p>Many thanks for help.</p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>Problem solved. Query should look like this one</p>
  
  <pre><code>import java.io.Serializable;
  import java.util.List;
  
  import javax.persistence.Entity;
  import javax.persistence.GeneratedValue;
  import javax.persistence.GenerationType;
  import javax.persistence.Id;
  import javax.persistence.NamedQuery;
  import javax.persistence.OneToMany;
  
  @Entity
  @NamedQuery(name = "Car.findAll", query = "SELECT DISTINCT c FROM Car c LEFT JOIN c.reservations r WHERE "
          + "c.type=:type AND c.dailyPrice&lt;=:dailyPrice AND ((r.dateOfRent &lt; :dateOfRent AND r.dateOfReturn &lt; :dateOfRent) OR "
          + "(r.dateOfRent &gt; :dateOfReturn) OR (r.dateOfRent IS NULL))")
  public class Car implements Serializable {
      private static final long serialVersionUID = 1L;
  
      @Id
      @GeneratedValue(strategy = GenerationType.IDENTITY)
      private Long id;
      private String producer;
      private String model;
      private Integer seatsNumber;
      private String type;
      private String registrationNumber;
      private Double dailyPrice;
      private String description;
      @OneToMany(mappedBy = "car")
      private List&lt;Reservation&gt; reservations;
  </code></pre>