gpt4 book ai didi

querydsl - 如何将涉及连接的 JPAQuery 对象转换为谓词?

转载 作者:行者123 更新时间:2023-12-02 01:16:11 25 4
gpt4 key购买 nike

我的查询看起来像这样 -

            @Override
public Page<Country> findPaginatedCountries(String country, Optional<String> status, Pageable pageable) {

QCountry qCountry= QCountry.someObject;
QActiveCountry qActiveCountry = QActiveCountry.activeCountry;

JPAQuery jpaQuery = new JPAQuery(entityManager);

QueryBase queryBase = jpaQuery.from(qCountry).innerJoin(qActiveCountry).fetch()
.where(qCountry.codeLeft.country.upper().eq(country.toUpperCase()))
.where(qCountry.codeRight.country.upper().eq(country.toUpperCase()));



if(status.isPresent()){
queryBase = queryBase.where(qActiveCountry.id(qCountry.active.id))
.where(qActiveCountry.status.upper().eq(status.get().toUpperCase()));
}
.......}

我可以改写一个谓词,这会导致相同的响应吗?
 Predicate predicate= qCountry.id.eq(qActiveCountry.id).and(qCountry.codeLeft.country.upper().eq(country.toUpperCase())).and(qCountry.codeRight.country.upper().eq(country.toUpperCase()));

最佳答案

是的你可以。似乎您正在使用 Spring Data。只需创建一个新的存储库接口(interface),或使用 QueryDslPredicateExecutor 扩展现有的 JPARepository 类型接口(interface),例如:

@Repository
public interface CountryRepository extends JpaRepository<Country, Long>,
QueryDslPredicateExecutor<Country>

现在你可以像这样传递你的谓词:
Predicate countryExpression= qCountry.id.eq(qActiveCountry.id).and(qCountry.codeLeft.country.upper().eq(country.toUpperCase())).and(qCountry.codeRight.country.upper().eq(country.toUpperCase()));
CountryRepository.findAll(countryExpression, pageable);

关于querydsl - 如何将涉及连接的 JPAQuery 对象转换为谓词?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42501147/

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