gpt4 book ai didi

subquery - querydsl 在子查询中使用父查询实体路径

转载 作者:行者123 更新时间:2023-12-05 05:26:13 25 4
gpt4 key购买 nike

我试图在 querydsl 中表示以下查询:

select a1.agreement_id, ad1.agreement_detail_id
from agreement a1
inner join agreement_details ad1 on a1.agreement_id=ad1.agreement_id
where ad1.transaction_id in (
select max(ad2.transaction_id)
from agreement_details ad2
inner join agreement a2 on ad2.agreement_id=a2.agreement_id
where
ad2.transaction_id<=1234
and a2.agreement_id=a1.agreement_id
and a2.entity_split_id=5678
)
order by a1.agreement_id asc

但是,我无法让 querydsl 在子查询的 where 子句中使用来自父查询的表。例如,如果我编写如下代码:

QAgreement a1 = QAgreement.agreement;
QAgreementDetails ad1 = QAgreementDetails.agreementDetails;
QAgreement a2 = QAgreement.agreement;
QAgreementDetails ad2 = QAgreementDetails.agreementDetails;
HibernateQuery query = getHibernateQuery();
query.from(a1)
.innerJoin(a1.agreementDetailsesByAgreementId, ad1)
.fetch()
.where(ad1.transactionId.in(
new JPASubQuery().from(ad2)
.innerJoin(ad2.agreementByAgreementId, a2)
.where(ad2.transactionId.loe(maxTransactionId))
.where(a1.agreementId.eq(a2.agreementId))
.where(a2.entitySplitByEntitySplitId.entitySplitId.eq(entitySplitId))
.list(ad2.transactionId.max())))
.orderBy(a1.agreementId.asc());

并执行它,结果SQL看起来像

select  -- Some columns removed for brevity
agreement0_.agreement_id as agreemen1_0_0_,
agreementd1_.agreement_detail_id as agreemen1_11_1_
from
agreement agreement0_
inner join
agreement_details agreementd1_
on agreement0_.agreement_id=agreementd1_.agreement_id
where
agreementd1_.transaction_id in (
select
max(agreementd2_.transaction_id)
from
agreement_details agreementd2_
inner join
agreement agreement3_
on agreementd2_.agreement_id=agreement3_.agreement_id
where
agreementd2_.transaction_id<=1234
and agreement3_.agreement_id=agreement3_.agreement_id
and agreement3_.entity_split_id=5678
)
order by
agreement0_.agreement_id asc

您可以看到 Agreement EntityPath a1 的别名没有在子查询中使用,而是被 Agreement EntityPath a2 的别名替换。我还需要在 querydsl 中做些什么才能生成此查询吗?

最佳答案

问题是你没有使用唯一变量

QAgreement a1 = QAgreement.agreement;
QAgreementDetails ad1 = QAgreementDetails.agreementDetails;
QAgreement a2 = QAgreement.agreement;
QAgreementDetails ad2 = QAgreementDetails.agreementDetails;

在这种情况下,a1 和 a2 以及 ad1 和 ad2 指的是同一路径。

Java 代码中的变量名无关紧要,您需要像这样使用它们

QAgreement a1 = new QAgreement("a1");
QAgreementDetails ad1 = new QAgreementDetails("ad1");
QAgreement a2 = new QAgreement("a2");
QAgreementDetails ad2 = new QAgreementDetails("ad2");

关于subquery - querydsl 在子查询中使用父查询实体路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27203932/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com