gpt4 book ai didi

java - 使用 JPA Criteria API 生成正确和/或条件

转载 作者:行者123 更新时间:2023-12-04 12:18:47 25 4
gpt4 key购买 nike

我正在尝试在“或”和“和”条件中生成带有多个括号的查询,但没有生成内部括号。
外部谓词的括号正确生成,但不是内部谓词的括号。
代码:

    CriteriaBuilder criteriaBuilder = this.getEntityManager().getCriteriaBuilder();
CriteriaQuery<ConfigurationKey> query = criteriaBuilder.createQuery(ConfigurationKey.class);
Root<ConfigurationKey> configurationKeyRoot = query.from(ConfigurationKey.class);
Join<ConfigurationKey, Customer> configurationKeyCustomerJoin = configurationKeyRoot.join(ConfigurationKey_.customer);

final List<Predicate> predicates = new ArrayList<>();
predicates.add(criteriaBuilder.equal(configurationKeyCustomerJoin.get(Customer_.externalId), customerId));
predicates.add(criteriaBuilder.equal(configurationKeyRoot.get(ConfigurationKey_.configType), configType));

final List<Predicate> orPredicates = new ArrayList<>();
keys.forEach((x, y) -> {
orPredicates.add(
criteriaBuilder.and(
criteriaBuilder.equal(configurationKeyRoot.get(ConfigurationKey_.keyType), x),
criteriaBuilder.equal(configurationKeyRoot.get(ConfigurationKey_.keyValue), y)
)
);
}

);

predicates.add(criteriaBuilder.or(orPredicates.toArray(new Predicate[orPredicates.size()])));
query.where(criteriaBuilder.and(predicates.toArray(new Predicate[predicates.size()])));
query.select(configurationKeyRoot);
TypedQuery<ConfigurationKey> typedQuery = this.getEntityManager().createQuery(query);

生成的查询:
select
configurat0_.id as id1_1_,
configurat0_.createdAt as createdA2_1_,
configurat0_.updatedAt as updatedA3_1_,
configurat0_.configType as configTy4_1_,
configurat0_.customerId as customer7_1_,
configurat0_.keyType as keyType5_1_,
configurat0_.keyValue as keyValue6_1_
from
configuration_keys configurat0_
inner join
customers customer1_
on configurat0_.customerId=customer1_.id
where
customer1_.externalId=?
and configurat0_.configType=?
and (
configurat0_.keyType=?
and configurat0_.keyValue=?
or configurat0_.keyType=?
and configurat0_.keyValue=?
)

预期查询:
select
configurat0_.id as id1_1_,
configurat0_.createdAt as createdA2_1_,
configurat0_.updatedAt as updatedA3_1_,
configurat0_.configType as configTy4_1_,
configurat0_.customerId as customer7_1_,
configurat0_.keyType as keyType5_1_,
configurat0_.keyValue as keyValue6_1_
from
configuration_keys configurat0_
inner join
customers customer1_
on configurat0_.customerId=customer1_.id
where
customer1_.externalId=?
and configurat0_.configType=?
and (
(configurat0_.keyType=?
and configurat0_.keyValue=?)
or
(configurat0_.keyType=?
and configurat0_.keyValue=?)
)

请指出任何错误。

最佳答案

Specifications<PcPlacement> specification = Specifications.where(null);
Specifications<PcPlacement> specificationInner = Specifications.where(null);

specificationInner = specificationInner.or(buildReferringEntitySpecificationWithContains(
criteria.getUserFullName(), PcPlacement_.pcUser, PcUser_.fullName));

specificationInner = specificationInner.or(buildReferringEntitySpecificationWithContains(
criteria.getUserEmailId(), PcPlacement_.pcUser, PcUser_.emailId));

specification = specification.and(specificationInner);

关于java - 使用 JPA Criteria API 生成正确和/或条件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40872949/

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