分组为JPA

时间:2014-06-16 11:26:43

标签: sql jpa

使用纯SQL:

select e.* 
from (
  select type, source, max(timestamp) as maxtimestamp 
  from handelse 
  group by type, source
) other join event e 
  on e.type=other.type and 
     e.source=other.source and 
     e.timestamp = other.maxtimestamp 
  join eventpost ep on e.id = ep.event_id;

带有最新时间戳的事件记录。糟糕的是,JPA不允许在from子句中进行子选择。任何想法如何重新编写查询到JPA可以吞下的东西?底层数据库是Oracle。

1 个答案:

答案 0 :(得分:0)

在玩弄后我将聚合移动到where子句:

select e 
  from Event e left join fetch e.eventpost 
where e.timestamp=(select max(o.timestamp) 
                   from Event o 
                   where o.type=e.type and 
                   o.source=e.source);