我对 Hibernate 有点困惑。 我的问题如下:
我的数据库表中有一个对象,该对象与不同的评论相关联(存储在另一个表中)
在 HQL 中,我想选择表中的这些对象,但前提是与该对象关联的最后一条注释具有特定状态(最后一条注释是具有最高 ID 的注释)。一条评论仅与一个对象关联。
此请求工作正常:
select distinct myObject from org.MyClass myInstance
join myObject.comments comment
where comment.status in (:theListOfStatusThatIWant)
但是该请求没有达到我想要的效果...
我已经尝试过这个:
select distinct myObject from org.MyClass myInstance
join myObject.comments comment
where comment.status in (:theListOfStatusThatIWant)
order by comment.id desc
但是没有成功......(请求工作正常,但没有达到我想要的效果)
有人可以帮我吗?
最佳答案
您需要表达您想要最后一条评论的事实,如下所示:
select distinct myObject from org.MyClass myInstance
join myObject.comments comment
where comment.status in (:theListOfStatusThatIWant)
and comment.id =
(select max(c1.id) from Comment c1 where c1 member of myObject.comments)
关于 hibernate HQL : where clause with list,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4817628/