JPQL 查询列表中的参数
JPQL query for parameter in list
我有两个 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))
我有两个 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))