如何用HQL表达以下SQL

发布于 2025-01-03 04:46:36 字数 283 浏览 1 评论 0原文

如何用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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

小红帽 2025-01-10 04:46:36

AFAIK,不可能直接做到这一点。但有一个简单的解决方法:

String hql = "select r from VwReportAlert r where r.user.id = 147"
             + " order by r.eventCreationDate desc";
List<VwReportAlert> result = session.createQuery(hql).setMaxResults(25).list();
Collections.sort(result, Collections.reverseOrder(new ByAlertIdComparator()));
return result;

当然,列表在 Java 中是排序的,但排序很容易,而且列表大小非常小。

AFAIK, it's not possible to do it directly. But there's a simple workaround:

String hql = "select r from VwReportAlert r where r.user.id = 147"
             + " order by r.eventCreationDate desc";
List<VwReportAlert> result = session.createQuery(hql).setMaxResults(25).list();
Collections.sort(result, Collections.reverseOrder(new ByAlertIdComparator()));
return result;

Sure, the list is sorted in Java, but the sort is easy, and the list size is very small.

为人所爱 2025-01-10 04:46:36

ROWNUM 是特定于供应商的功能(特别是 Oracle)。 SQLServer 在使用伪限制创建查询时有一些非常相似的东西:查询必须包装在 SELECT ROW_NUMBER() OVER ... 中,这不是您在 Hibernate 中通常无法做到的事情,除非诉诸原始 SQL 。

我建议做的是检查 供应商特定方言您用于支持此功能。他们为所有主要供应商提供了一个,包括:

  • PostgreSQL
  • MySQL
  • Oracle
  • SQLServer
  • Sybase
  • 等。

还要确保您在休眠配置中实际上使用了正确的方言。

从仔细阅读源代码来看,Hibernate 至少从 Oracle 8 开始就支持 Oracle 的 ROWNUM 函数。

编辑:
这应该会产生您正在寻找的内容。我冒昧地将数据库字段/表转换为适合 Java 的名称,因为看起来您使用的是原始 SQL 而不是 Java 对象(表“vw_report_alert”-> VwReportAlert,列“user_id”-> userId,列“ Alert_id" -> id,列“event_creation_date”-> eventCreationDate)

getSession().createQuery(
  "select r from VwReportAlert r " +
  "where r.userId = :userId " +
  "order by r.eventCreationDate desc, r.id desc")
.setInteger("userId", 147)
.setMaxResults(25)
.list();

ROWNUM is vendor-specific functionality (Oracle, specifically). SQLServer has something very similar when creating queries using pseudo-limits: queries have to be wrapped in SELECT 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:

  • PostgreSQL
  • MySQL
  • Oracle
  • SQLServer
  • Sybase
  • etc.

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)

getSession().createQuery(
  "select r from VwReportAlert r " +
  "where r.userId = :userId " +
  "order by r.eventCreationDate desc, r.id desc")
.setInteger("userId", 147)
.setMaxResults(25)
.list();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文