SQL查询TO JPQL之一

时间:2017-04-29 14:22:26

标签: sql jpa jpql

我正在尝试转换此SQL查询:

SELECT *
    FROM event
    WHERE idEvent IN (SELECT idEventfk FROM userevent WHERE absence='1');

进入JPQL:

SELECT e FROM Event e where e.idEvent
IN (SELECT ue.idEventfk FROM userevent ue WHERE ue.absence='1'

但结果正在返回null。对这个错误有什么看法吗?

3 个答案:

答案 0 :(得分:1)

如果您在Event实体中有相关实体的对象,那么您可以加入它们以获得所需的结果:

select e from Event e join e.userevent ue where ue.absence = '1'

答案 1 :(得分:1)

试试这个:

SELECT e.* FROM Event e where e.idEvent
IN (SELECT ue.idEventfk FROM userevent ue WHERE ue.absence='1')

答案 2 :(得分:1)

您可以使用EXISTS子句:

SELECT e FROM Event e 
where exists 
  (SELECT ue FROM userevent ue WHERE e.idEvent = ue.idEventfk and ue.absence='1')