这是JPA/JPQL的后续问题: SELECT 子句中不允许使用 AS 标识符。
这是 JPQL 查询:
SELECT NEW com.domain.project.view.StandingsStatLine(
ro.id
, cl.name
, te.ordinalNbr + 1
, pa.wasWithdrawn
, SUM(CASE WHEN paf.wasWithdrawn = FALSE AND paa.wasWithdrawn = FALSE AND scf.finalScore IS NOT NULL THEN 1 ELSE 0 END) // g = games
, SUM(CASE WHEN paf.wasWithdrawn = FALSE AND paa.wasWithdrawn = FALSE AND scf.finalScore > sca.finalScore THEN 1 ELSE 0 END) // w = wins
, SUM(CASE WHEN paf.wasWithdrawn = FALSE AND paa.wasWithdrawn = FALSE AND scf.finalScore < sca.finalScore THEN 1 ELSE 0 END) // l = losses
, ...
, <very complex multi-line expression> // nrp = normalized ranking points
)
FROM Club cl
JOIN cl.teams te
JOIN te.rosters ro
JOIN ro.season se
JOIN ro.participations pa
JOIN pa.group gr
JOIN gr.round rd
JOIN rd.subCompetition sc
JOIN sc.competition cn
JOIN gr.games ga
JOIN ga.scores scf
JOIN ga.scores sca
JOIN scf.roster rof
JOIN sca.roster roa
JOIN rof.participations paf
JOIN roa.participations paa
WHERE ...
GROUP BY ...
ORDER BY pa.wasWithdrawn, nrp DESC, w DESC, g DESC, cl.shorthand
这与没有... AS ...
表达式的上一个问题中的陈述相同。
对于有兴趣的人:
- nrp 是“标准化排名点”。这是根据所玩游戏可获得的最大分数的简单百分比。背景是,一支打了 10 场比赛的球队的总积分数(每输一场得 1 分)比一支只打了 6 场比赛的球队高得多。这是实际的排名指标。w 和 g 在代码中进行了解释。
- scf 和 sca 是支持和反对的得分(在 中过滤
WHERE
)
问:
您如何ORDER BY
在 JPQL 中计算这些属性?我经常遇到这种情况,但是由于... AS ...
SELECT 中的 JPQL 似乎无效(Hibernate 可以处理它们),您通常如何解决这个问题?