2015-12-03 65 views
0

我想在hql中進行此查詢,所以我不必將查詢結果映射到我的java對象,但嘗試不同的選項後,我仍然無法這行得通。有沒有可能以任何方式在hql中執行此查詢?查詢是:如何將sql轉換爲hql加入和最大

select t.* 
from part_movement t 
    join 
    (select id_block_movement, max(start_date) as somedate 
     from part_movement 
     where id_area_final_destination = 1 
    and ((id_area_destination != id_area_final_destination and id_user_receiver is not null) or 
(id_area_destination = id_area_final_destination and id_user_receiver is null)) 
     group by id_block_movement 
    ) s 
    on s.id_block_movement = t.id_block_movement 
    and s.somedate= t.start_date; 

感謝高級!

回答

0

好吧,我終於明白了!這是:(感覺有線回答自己,沒有任何別人的回答)

Query q2 = s.createQuery(" from PartMovement t " 
       + " WHERE t.areaByIdAreaFinalDestination = ? " 
       + " AND t.startDate IN (select max(b.startDate) from PartMovement b " 
       + " WHERE ((b.areaByIdAreaDestination != b.areaByIdAreaFinalDestination and b.usersByIdUserReceiver is not null) OR " 
       + " (b.areaByIdAreaDestination = b.areaByIdAreaFinalDestination and b.usersByIdUserReceiver is null)) " 
               + " group by b.idBlockMovement)");