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

标签 querydsl

我的查询看起来像这样 -

            @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/

相关文章:

mongodb - QueryDsl MongoRepository 投影

querydsl - 初始化路径时出现 NullPointerException

java - 按计数查询dsl转换器组

spring - QuerydslBinderCustomizer 的多个别名

java - QueryDsl 集模式

mysql - QueryDSL 中 Case Builder 表达式的总和

spring - 如何使用 Querydsl 和 Spring Data 轻松实现 'REST API query language' 以过滤实体?

java - 使用QueryDSL消除hibernate hql parser

java - 在带有 Spring Data 的 Mongo 实体中使用 jodatime

neo4j - Cypher-QueryDSL : 'My "Q"classes cannot be resolved' compile error (e. g。 Q人)