Hibernate 6.2引入了CTE支持,但不清楚如何在单个HQL查询中使用多个CTE。假设我有City实体和以下CTE查询:
TypedQuery<Integer> typedQuery = em.createQuery(
"""
with max_cities as (
SELECT id id from City c ORDER BY population DESC
LIMIT 20
), min_cities as (
SELECT id id from City c ORDER BY population ASC
LIMIT 20
)
SELECT m1.id from max_cities m1 join min_cities m2
on m1.id = m2.id
""",
Integer.class);
它包含两个CTE(min_cities和max_cities)。而Hibernate并没有抱怨这一点。但是错误信息在这里非常清楚:
Caused by: java.lang.IllegalArgumentException: Could not resolve entity reference: min_cities
at org.hibernate.metamodel.model.domain.internal.JpaMetamodelImpl.resolveHqlEntityReference(JpaMetamodelImpl.java:166)
所以Hibernate允许在FROM子句中使用第一个CTE,但在JOIN子句中需要实体(而不是CTE)。然而,MySQL文档指出,加入两个CTE是非常法律的的:
WITH
cte1 AS (SELECT a, b FROM table1),
cte2 AS (SELECT c, d FROM table2)
SELECT b, d FROM cte1 JOIN cte2
WHERE cte1.a = cte2.c;
Hibernate支持加入CTE吗?
1条答案
按热度按时间vwkv1x7d1#
这看起来像是ExpectingEntityJoinDelegate#consumeIdentifier中的一个bug:
顺便说一句,交叉连接确实按预期工作,即: