2017-02-28 8 views
2

私のクエリは次のようになります -ジョインを含むJPAQueryオブジェクトをPredicateに変換するにはどうすればよいですか?

  @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())); 

答えて

1

はいできます。あなたは春のデータを使用しているようです。

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); 
:今、あなたは同じようにあなたの述語を渡すことができ

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

:ちょうど新しいリポジトリのインターフェースを作成するなどQueryDslPredicateExecutorで既存のJPARepositoryタイプのインタフェースを拡張

関連する問題