如何用HQL表达以下SQL
如何用HQL表达以下SQL。
select * from (
select *
from vw_report_alert r
where r.user_id=147
order by r.event_creation_date DESC
)
where rownum <= 25
order by alert_id DESC
谢谢
How to express the following SQL in HQL.
select * from (
select *
from vw_report_alert r
where r.user_id=147
order by r.event_creation_date DESC
)
where rownum <= 25
order by alert_id DESC
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
AFAIK,不可能直接做到这一点。但有一个简单的解决方法:
当然,列表在 Java 中是排序的,但排序很容易,而且列表大小非常小。
AFAIK, it's not possible to do it directly. But there's a simple workaround:
Sure, the list is sorted in Java, but the sort is easy, and the list size is very small.
ROWNUM
是特定于供应商的功能(特别是 Oracle)。 SQLServer 在使用伪限制创建查询时有一些非常相似的东西:查询必须包装在 SELECT ROW_NUMBER() OVER ... 中,这不是您在 Hibernate 中通常无法做到的事情,除非诉诸原始 SQL 。我建议做的是检查 供应商特定方言您用于支持此功能。他们为所有主要供应商提供了一个,包括:
还要确保您在休眠配置中实际上使用了正确的方言。
从仔细阅读源代码来看,Hibernate 至少从 Oracle 8 开始就支持 Oracle 的 ROWNUM 函数。
编辑:
这应该会产生您正在寻找的内容。我冒昧地将数据库字段/表转换为适合 Java 的名称,因为看起来您使用的是原始 SQL 而不是 Java 对象(表“vw_report_alert”-> VwReportAlert,列“user_id”-> userId,列“ Alert_id" -> id,列“event_creation_date”-> eventCreationDate)
ROWNUM
is vendor-specific functionality (Oracle, specifically). SQLServer has something very similar when creating queries using pseudo-limits: queries have to be wrapped inSELECT ROW_NUMBER() OVER ...
which isn't something you can normally do in Hibernate without resorting to raw SQL.What I would recommend doing is checking the vendor-specific dialect you're using for support of this functionality. They have one for all the major vendors, including:
Also make sure you're actually using the correct dialect in your hibernate configuration.
From perusing the source, it appears Hibernate has supported Oracle's
ROWNUM
function since at least Oracle 8.EDIT:
This should produce what you're looking for. I took the liberty of converting DB fields/tables to Java-appropriate names because it looks like you're using raw SQL instead of Java objects (table "vw_report_alert" -> VwReportAlert, column "user_id" -> userId, column "alert_id" -> id, column "event_creation_date" -> eventCreationDate)