2014-06-16 34 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);