【问题标题】:Please help converting this SQL to JPQL, problem with subquery (EclipseLink v2)请帮助将此 SQL 转换为 JPQL,子查询有问题(EclipseLink v2)
【发布时间】:2011-07-30 03:30:57
【问题描述】:

我有好几天试图解决这个问题。

我需要在子查询中有一个 ORDER BY 和一个 LIMIT,这是原生 SQL:

SELECT
    ope.idOperacion AS OperationID,
    est.nombre AS LastStatusName
FROM
    SSCM_4.Operaciones4 ope
INNER JOIN SSCM_4.BitacoraOperaciones4 bita
ON
    bita.operacion = ope.idOperacion
INNER JOIN SSCM_4.Estatus4 est
ON
    est.idEstatus = bita.estatus
WHERE
    ope.idOperacion = 54
AND bita.idBitacoraOperacion =
    (
        SELECT
            BO2.idBitacoraOperacion
        FROM
            SSCM_4.BitacoraOperaciones4 BO2
        INNER JOIN SSCM_4.Estatus4 EST2
        ON
            (
                BO2.Estatus = EST2.idEstatus
            )
        WHERE
            BO2.Operacion = ope.idOperacion
        ORDER BY
            EST2.Prioridad DESC,
            BO2.FechaOperacion DESC,
            BO2.idBitacoraOperacion ASC LIMIT 1
    )

但问题是 JPQL 在子查询中不支持 ORDER BY,无论是 LIMIT。

业务逻辑是这样的:子查询必须返回操作的最后状态,主要由操作日志历史记录行中的优先级和日期定义,这就是我需要 ORDER BY 和 LIMIT 子句的原因。

我在 JPQL 中需要这个,因为我已经为动态查询开发了一个 API,它是一个查询构建器,用于我需要使用的 JPQL。

提前谢谢你。

【问题讨论】:

    标签: sql jpa sql-order-by jpql nativequery


    【解决方案1】:

    JPQL 不支持此功能。您需要使用原生 SQL 查询,或者改写查询,或者使用多个查询。

    您可能会更改子查询以使用 max 而不是 orderby。

    您可以记录错误/增强功能以​​将 orderby 支持添加到子选择中,但限制不是标准 SQL,也不是 JPQL 的一部分,因为它是特定于数据库的。

    【讨论】:

      猜你喜欢
      • 2011-03-25
      • 2020-07-28
      • 1970-01-01
      • 2021-10-09
      • 2020-10-05
      • 2011-07-05
      • 1970-01-01
      • 2014-01-17
      • 2011-09-29
      相关资源
      最近更新 更多