java - 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 查询来提取以下请求:
- 如果参数为
false
空,则提取所有请求,hotelBookings
并且每个预订都有参数的所有请求都jointCheckIn
设置为false
- 如果一个参数是
true
然后提取具有一个或多个预订的所有请求jointCheckIn
设置为true
我写了这样的东西
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
参数写一个工作条件
解决方案
评论中建议的解决方案
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))
推荐阅读
- python - 如何在 Win 上下载 Linux python 版本模块?
- elasticsearch - ElasticSearch 查询优化
- cmake - 简单的 CMAKE 教程有问题
- azure - 在模板参数中使用任务输出变量
- java - org.springframework.beans.factory.UnsatisfiedDependencyException:创建名为“brightspotService”的bean时出错
- ios - AVPlayerViewController 问题和疑问
- firebase - 我可以检索但不能有用地使用来自 firebase 数据库的数据
- mongodb - MongoDB聚合对象数组
- java - 检测具有相同完全限定名称的多个类
- python - 比较熊猫数据框中的两个并排列并同时为选定的单元格着色