简体   繁体   English

JPQL 查询列表中的参数

[英]JPQL query for parameter in list

I have two JPA-entities:我有两个 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;
}

and I tried to write a JPQL query to extract requests that:我尝试编写一个 JPQL 查询来提取以下请求:

  • If the parameter is false then extract all requests with empty hotelBookings and all requests where every booking have parameter jointCheckIn is set to false如果参数为false ,则提取所有带有空hotelBookings的请求,并且每个预订都将参数jointCheckIn设置为false的所有请求
  • If a parameter is true then extract all requests that have one or more bookings with jointCheckIn is set to true如果参数为true ,则提取具有一个或多个预订的所有请求, jointCheckIn设置为true

I wrote something like this我写了这样的东西

select e from nk$BusinessTripRequest e join e.hotelBookings hb select e 从 nk$BusinessTripRequest e 加入 e.hotelBookings hb
where (true =? and e.hotelBookings is not empty and hb.jointCheckIn = true)其中(true =?且 e.hotelBookings 不为空且 hb.jointCheckIn = true)
or (false =? and e.hotelBookings is empty)或(false =? 并且 e.hotelBookings 为空)

It works well when parameter is true because of the first condition.由于第一个条件,当参数为true时效果很好。 But I can't write a working condition for false parameter但是我不能为false参数写一个工作条件

solution suggested from comments评论中建议的解决方案

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))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM