简体   繁体   English

休眠hql getMaxVersion

[英]hibernate hql getMaxVersion

I have a table with the following structure: 我有一个具有以下结构的表:

ReportId 报告编号
Version
Title 标题
..... .....

I want to use HQL to fetch the newest version of the report by id. 我想使用HQL通过ID获取报告的最新版本。 Would the following query work? 以下查询有效吗?

from Report where reportId = :reportId and version = (select max(version) from Report where reportId = :reportId)

Is it possible to retrieve the row with the maximum version without using a sub-select? 是否可以在不使用子选择的情况下检索具有最高版本的行? Is the above sub-select even legal in hibernate HQL? 在休眠HQL中,上述子选择是否合法?

You can order by version and use setMaxResults on the query (set it to 1). 您可以按版本排序,并在查询上使用setMaxResults(将其设置为1)。 As for the other question, subselects like this are legal in hql. 至于另一个问题,像这样的子选择在hql中是合法的。

Try this query: 试试这个查询:

    from Report where reportId = :reportId order by version desc 

with setMaxResults=1 与setMaxResults = 1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM