如何使用LEFT JOIN创建JPA NamedQuery

时间:2017-05-14 21:05:59

标签: java mysql hibernate jpa join

我有两个实体:汽车和预订。我想用LEFT JOIN创建命名查询。我试图像这里所描述的那样How to create JPA query with LEFT OUTER JOIN,但它不起作用。你知道我的查询有什么问题吗?我想展示有NULL预订的汽车。无论如何,即使使用JOIN它也不起作用。启动应用程序后,我有一个错误:

Caused by: org.hibernate.HibernateException: Errors in named queries: Car.findAll
    at org.hibernate.internal.SessionFactoryImpl.<init>(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

原则上我想在MySQL工作中实现这个查询

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 < 'date1' AND r.date_of_return < 'date1') OR (r.date_of_rent > 'date2') OR r.date_of_rent IS NULL;

汽车实体

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<Reservation> reservations;

预订实体

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;

非常感谢您的帮助。

更新

问题解决了。查询应该看起来像这个

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<=:dailyPrice AND ((r.dateOfRent < :dateOfRent AND r.dateOfReturn < :dateOfRent) OR "
        + "(r.dateOfRent > :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<Reservation> reservations;

1 个答案:

答案 0 :(得分:0)

SELECT c FROM Car c LEFT JOIN c.reservation r WHERE c.producer=:producer "
        + "AND c.type=:type AND c.dailyPrice=:dailyPrice

此查询中存在错误c.reservation需要更改为c.reservations

  

我想展示有NULL保留的汽车。

你不能这样做。尝试从此查询开始:

select c from Car c where not exists (
    select r.id from Reservation r where r.car.id = c.id
)