有两种模型:Event
和Session
,它们具有一对多的关系。
我想基于这样的SQL使用JPA Criteria API创建谓词:
SELECT event.* FROM events JOIN (
SELECT event.id, MIN(session.start) AS start FROM
events JOIN sessions ON events.id = sessions.event_id
GROUP BY event.id
) TMP on events.id = TMP.id
WHERE start >= '2018-08-01 00:00:00';
我引用此链接:http://www.thejavageek.com/2014/04/28/criteria-group-clause/通过子查询构建组。
但是我由于SubQuery
没有multiselect
函数而陷入困境,如何通过子查询基于组构建谓词?
答案 0 :(得分:0)
请尝试以下查询,并使用in
函数和min
子句通过汇总日期的having
条件
select event from Event event
and event.session.start in
(select min(nestedEvent.session.start) minDates
FROM Event nestedEvent
group by nestedEvent.id
having minDates >= '2018-08-01 00:00:00')