hibernate HQL : where clause with list

标签 hibernate jakarta-ee hql

我对 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/

相关文章:

java - 从 JSP 文本框将输入输入到矩阵?

java - 在 JBos 的 servlet 中使用 TCP 套接字

java - 导航规则/案例问题

nhibernate - 如何在没有 queryOver 的情况下使用 nHibernate 从 TABLE 中选择 count(*)?

java - Hibernate 缓存的例子?

Hibernate:在@Formula 注释中使用 HQL?

java - Spring/JPA项目无法找到逻辑名称为: email的列

hadoop - 使用STRUCT进行的此简单Hive查询的语法错误在哪里?

java - 如何获取属于一项调查的所有问题?

mysql - SQL异常: Column not found when there is no such column in the query