带有元组条件的 QueryDSL 和 SubQuery
QueryDSL and SubQuery with Tuple condition
我正在尝试在 QueryDSL 中编写查询以获取按其父 ID 分组的 table 中最旧的元素。
SQL 等价物应该是:
SELECT a.* FROM child a
INNER JOIN
(
SELECT parentId, MAX(revision) FROM child GROUP BY parentId
) b
ON (
a.parentId = b.parentId AND a.revision = b.revision
)
现在在 QueryDSL 中我被语法困住了。
JPQLQuery<Tuple> subquery = JPAExpressions
.select(child.parent, child.revision.max())
.from(child)
.groupBy(child.parent);
HibernateQuery<Child> query = new HibernateQuery<>(session);
query.from(child)
.where(child.parent.eq(subquery.???).and(child.revision.eq(subquery.???))));
如何使用子查询编写此查询?
table 看起来像这样:
___parent___ (not used in this query, but exists)
parentId
P1 | *
P2 | *
P3 | *
___child___
parentId | revision
P1 | 1 | *
P1 | 2 | *
P1 | 3 | *
P2 | 2 | *
P2 | 3 | *
P3 | 1 | *
___result from child, the highest revision for each parentId___
P1 | 3 | *
P2 | 3 | *
P3 | 1 | *
到目前为止我尝试了什么:
.where(JPAExpressions.select(child.parent,child.revision).eq(subquery));
-> org.hibernate.hql.internal.ast.QuerySyntaxException: unexpected end of subtree
还有很多语法错误...
我暂时使用脏循环,因为我还没有找到解决方案。
在 JPA 中,子查询只能出现在 where 部分。
这是我对你的查询的看法
select(child).from(child).where(child.revision.eq(
select(child2.revision.max())
.from(child2)
.where(child2.parent.eq(child.parent))
.groupBy(child2.parent))).fetch()
您可以使用 Expressions.list()
为 in 子句指定多个列:
query.from(child).where(Expressions.list(child.parent, child.revision).in(subquery));
另一种方法是使用 innerJoin()
,就像您原来的 SQL 一样。
Expressions.list(ENTITY.year, ENTITY.week).in(//
Expressions.list(Expressions.constant(1029), Expressions.constant(1)),
Expressions.list(Expressions.constant(1030), Expressions.constant(1)),
Expressions.list(Expressions.constant(1031), Expressions.constant(1))
将是您要查找的内容,但 QueryDSL 从中生成了错误的 SQL:
((p0_.year , p0_.week) in (1029 , 1 , (1030 , 1) , (1031 , 1)))
我正在尝试在 QueryDSL 中编写查询以获取按其父 ID 分组的 table 中最旧的元素。
SQL 等价物应该是:
SELECT a.* FROM child a
INNER JOIN
(
SELECT parentId, MAX(revision) FROM child GROUP BY parentId
) b
ON (
a.parentId = b.parentId AND a.revision = b.revision
)
现在在 QueryDSL 中我被语法困住了。
JPQLQuery<Tuple> subquery = JPAExpressions
.select(child.parent, child.revision.max())
.from(child)
.groupBy(child.parent);
HibernateQuery<Child> query = new HibernateQuery<>(session);
query.from(child)
.where(child.parent.eq(subquery.???).and(child.revision.eq(subquery.???))));
如何使用子查询编写此查询?
table 看起来像这样:
___parent___ (not used in this query, but exists) parentId P1 | * P2 | * P3 | * ___child___ parentId | revision P1 | 1 | * P1 | 2 | * P1 | 3 | * P2 | 2 | * P2 | 3 | * P3 | 1 | * ___result from child, the highest revision for each parentId___ P1 | 3 | * P2 | 3 | * P3 | 1 | *
到目前为止我尝试了什么:
.where(JPAExpressions.select(child.parent,child.revision).eq(subquery));
-> org.hibernate.hql.internal.ast.QuerySyntaxException: unexpected end of subtree
还有很多语法错误...
我暂时使用脏循环,因为我还没有找到解决方案。
在 JPA 中,子查询只能出现在 where 部分。
这是我对你的查询的看法
select(child).from(child).where(child.revision.eq(
select(child2.revision.max())
.from(child2)
.where(child2.parent.eq(child.parent))
.groupBy(child2.parent))).fetch()
您可以使用 Expressions.list()
为 in 子句指定多个列:
query.from(child).where(Expressions.list(child.parent, child.revision).in(subquery));
另一种方法是使用 innerJoin()
,就像您原来的 SQL 一样。
Expressions.list(ENTITY.year, ENTITY.week).in(//
Expressions.list(Expressions.constant(1029), Expressions.constant(1)),
Expressions.list(Expressions.constant(1030), Expressions.constant(1)),
Expressions.list(Expressions.constant(1031), Expressions.constant(1))
将是您要查找的内容,但 QueryDSL 从中生成了错误的 SQL:
((p0_.year , p0_.week) in (1029 , 1 , (1030 , 1) , (1031 , 1)))