2011-01-30 79 views
0

我有一個簡單的類,稱爲,如下:需要與HQL查詢幫助

public class Person { 
    private Integer id; 
    private String name; 
    private List<PersonState> states; 
    ... 
} 

而且每個PersonState的定義如下:

public class PersonState { 
    private Integer id; 
    private State state; 
    private Date date; 
    ... 
} 

而且國家是一個簡單的類,只包含一個ID名稱,我有所有的映射文件配置和工作。

我想那些其最後一PersonState(基於它的日期)具有國家ID = 5(例如),所以這將是HQL查詢什麼樣的? 謝謝!

回答

1

事情是這樣的:

SELECT p FROM Person p JOIN p.states ps1 
WHERE 
    ps1.date = (SELECT MAX(ps2.date) FROM PersonState ps2 WHERE ps2 MEMBER OF p.states) 
    AND ps1.state.id = ? 
+0

呀!我正是這個意思! – 2011-01-30 21:47:04