首页 > 解决方案 > JPQL 查询列表中的参数

问题描述

我有两个 JPA 实体:

public class BusinessTripRequest extends StandardEntity {
    @OneToMany(mappedBy = "businessTripRequest", fetch = FetchType.LAZY)
    @OnDelete(DeletePolicy.CASCADE)
    @Composition
    protected List<HotelBooking> hotelBookings;
}

public class HotelBooking extends StandardEntity {
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "BUSINESS_TRIP_REQUEST_ID")
    protected BusinessTripRequest businessTripRequest;

    @Column(name = "JOINT_CHECK_IN")
    protected Boolean jointCheckIn;
}

我尝试编写一个 JPQL 查询来提取以下请求:

我写了这样的东西

select e from nk$BusinessTripRequest e join e.hotelBookings hb
where (true = ? and e.hotelBookings is not empty and hb.jointCheckIn = true)
or (false = ? and e.hotelBookings is empty)

当参数是true因为第一个条件时,它工作得很好。但是我不能为false参数写一个工作条件

标签: javajpajpql

解决方案


评论中建议的解决方案

select e
from nk$BusinessTripRequest e
where (true = ? and e.id in (select hb1.businessTripRequest.id
                         from HotelBooking hb1
                         where hb1.jointCheckIn = true))
   or (false = ? and {E}.id not in (select hb1.businessTripRequest.id
                                    from nokia$HotelBooking hb1
                                    where hb1.jointCheckIn = true))

推荐阅读