2014-06-16 35 views
0

用普通的SQL:集团通过为JPA

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。

回答

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);