如何使用LEFT JOIN创建JPA NamedQuery

axzmvihb  于 2022-11-30  发布在  其他
关注(0)|答案(1)|浏览(139)

我有两个实体:汽车和预订。我想用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;
wd2eg0qa

wd2eg0qa1#

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
我想显示预订为空的汽车。
您不能这样做。请尝试从以下查询开始:

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

相关问题