提问者:小点点

如何使用@QueryInit在查询DSL查询中初始化嵌入对象的属性路径?


根据QueryDSL文档(http://www.querydsl.com/static/querydsl/4.0.8/reference/html_single/#d0e2250)在生成的Q类中,只有前两个级别的路径被初始化,因此如果对象A在@JoinColumn定义的@实体B中具有关系,则不能在查询中执行类似B.A.id的操作以从B中访问A的id。他们有一个@QueryInit注释,可以在我们需要更深入路径的情况下使用,但文档非常简短,我不知道它是如何使用的。现在,当我尝试在应用程序中运行查询时,我遇到了一个“无效路径”异常。是否有人使用@QueryInit来解决类似于我的问题?

查询where子句(即。在QueryDSL中)类似于< code>a.id.eq(b.a.id),其中a“嵌入”在b中

我看到的例外是:

"org.hibernate.hql.internal.ast.QuerySyntaxException: Invalid path: 'b.a.id' [select a from A a where a.id is not null and (a.id = b.a.id and a.amount <= sum(b.amount))]; nested exception is java.lang.IllegalArgumentException: org.hibernate.hql.internal.ast.QuerySyntaxException: Invalid path: 'b.a.id' [select a from A a where a.id is not null and (a.id = b.a.id and a.amountCents <= sum(b.amountCents))]",

我试图“翻译”成QueryDSL的查询如下:

SELECT * FROM A a
  WHERE a.amount <= (SELECT SUM(b.amount) FROM B b WHERE b.a_id=a.id);

共1个答案

匿名用户

谓词最终只是:

a.amount.loe(
JPAExpressions.select(b.amount.sum()).from(b).where(a.id.eq(b.a.id)))
)