Querydsl BooleanExpressions を返す静的メソッドを持つクラスを作成したので、これらの式を Spring Data リポジトリのfindAll()
またはfindOne()
メソッドに渡すだけでなく、アプリケーション全体でそれらを再利用できます。ここのように: http://www.petrikainulainen.net/programming/spring-framework/spring-data-jpa-tutorial-part-five-querydsl/ .
私が抱えている問題は、私のメソッドから返された BooleanExpression が、次善のように見えるクエリを生成していることです (少なくとも私の場合)。
BooleanExpression を返すメソッド:
public static BooleanExpression byExternalIdAndProviderId(
final String externalId, final Long providerId) {
QEListing listing = QEListing.eListing;
return listing.externalIds.any().externalId.equalsIgnoreCase(externalId)
.and(listing.externalIds.any().provider.id.eq(providerId));
}
生成されるクエリ:
select elisting0_.id as id1_24_,
elisting0_.address1 as address2_24_,
elisting0_.address2 as address3_24_,
elisting0_.business_name as business4_24_,
elisting0_.city_id as city10_24_,
elisting0_.created_date as created5_24_,
elisting0_.latitude as latitude6_24_,
elisting0_.longitude as longitud7_24_,
elisting0_.master_ext_id as master11_24_,
elisting0_.modified_date as modified8_24_,
elisting0_.state_id as state12_24_,
elisting0_.zip as zip13_24_,
elisting0_.zip_4 as zip9_24_,
elisting0_1_.vanity_name as vanity1_34_,
elisting0_2_.account_id as account1_2_
from listings elisting0_
left outer join vanity_names elisting0_1_ on elisting0_.id=elisting0_1_.listing_id
left outer join accounts_to_listings elisting0_2_ on elisting0_.id=elisting0_2_.listing_id
where (
exists (
select 1
from external_ids eexternali1_
where (
eexternali1_.id in (
select externalid2_.external_id
from listing_to_external_id externalid2_
where elisting0_.id=externalid2_.listing_id))
and lower(eexternali1_.external_id)='123456'
)
)
and (
exists (
select 1
from external_ids eexternali3_
where (
eexternali3_.id in (
select externalid4_.external_id
from listing_to_external_id externalid4_
where elisting0_.id=externalid4_.listing_id
)
)
and eexternali3_.provider_id=1
)
)
このようなものとは対照的に:
select elisting0_.id as id1_24_,
elisting0_.address1 as address2_24_,
elisting0_.address2 as address3_24_,
elisting0_.business_name as business4_24_,
elisting0_.city_id as city10_24_,
elisting0_.created_date as created5_24_,
elisting0_.latitude as latitude6_24_,
elisting0_.longitude as longitud7_24_,
elisting0_.master_ext_id as master11_24_,
elisting0_.modified_date as modified8_24_,
elisting0_.state_id as state12_24_,
elisting0_.zip as zip13_24_,
elisting0_.zip_4 as zip9_24_,
elisting0_1_.vanity_name as vanity1_34_,
elisting0_2_.account_id as account1_2_
from listings elisting0_
left outer join vanity_names elisting0_1_ on elisting0_.id=elisting0_1_.listing_id
left outer join accounts_to_listings elisting0_2_ on elisting0_.id=elisting0_2_.listing_id
join listing_to_external_id a on elisting0_.id = a.listing_id
join external_ids b on a.external_id = b.id
where lower(b.external_id) = '123456'
and b.provider_id = 1
BooleanExpressions の再利用性を提供し、Spring Data のリポジトリを使用しながら、より最適なクエリを生成する方法はありますか?