需要有关HQL查询的帮助

时间:2011-01-30 21:29:08

标签: hibernate hibernate-mapping hbm

我有一个简单的类,名为 Person ,如下所示:

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 name ,并且我已配置并运行所有映射文件。< / p>

我想让那些人员的最后 PersonState (基于它的 date )的状态包含状态 id = 5(例如),那么HQL查询会是什么样的? 谢谢!

1 个答案:

答案 0 :(得分: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 = ?