Querydsl: selfJoinがクエリにあるときにフェッチで問題が発生する

作成日 2019年04月25日  ·  4コメント  ·  ソース: querydsl/querydsl

テーブルに自己結合している場合があります。
queryFactory
.select(queryBase.getColumns())
.from(entityPath)
.leftJoin(QTodoEntity.todoEntity.parentLocation、QTodoEntity.todoEntity)
.leftJoin(QTodoEntity.locationEntity.country、QCountryEntity.countryEntity)
.where(queryBase.getFilters())
.orderBy(queryBase.getOrderBy())
.offset(queryBase.getOffset())
.limit(queryBase.getLimit())
。フェッチ()
。ストリーム()
.map(queryBase :: mapRow)
.collect(Collectors.toList());

左結合で作成されたQTodoEntityのエイリアスは、クエリのあらゆる場所で使用されます。 選択時に作成されたエイリアスを使用する必要がありますが。
クエリは次のようになります

todo1.id、todo1.name、todo1.custom、todo1.selfChildをTodoから選択します。todoOnSelect左外部結合Todo todo1 on todoOnSelect.selfChild = todo1.id

しかし、クエリは-
todoOnSelect.id、todoOnSelect.name、todoOnSelect.custom、todoOnSelect.selfChildをTodoから選択します。todoOnSelect左外部結合Todo todo1 on todoOnSelect.selfChild = todo1.id

最も参考になるコメント

このようなものを試してください-
QCategoryカテゴリ= QCategory.category;
QCategory subCategory = new QCategory( "sub");
query.from(category)
.leftJoin(category.subcategory、subCategory)
.where(category.type.equalsIgnoreCase( "A"))
.fetchCount();

これにより、subCategoryにエイリアスが与えられ、where句では、左側に結合されたエンティティではなく、メインエンティティが選択されます。

全てのコメント4件

これはバグではありません。

これはバグではありません。

これをどのように解決しましたか? 同じ問題が発生しています。where句で「from」エイリアスではなく「leftjoin」エイリアスが使用されており、結果が正しくありません。

私は得ています:
select count(sub.id) from category c
left join category sub on c.id = sub.id
where sub.type='A'

そのはず:
select count(c.id) from category c
left join category sub on c.id = sub.id
where c.type='A'

これがqueryDslです:
QCategory category = QCategory.category;
query.from(category)
.leftJoin(category.subcategory, QCategory.category)
.where(category.type.equalsIgnoreCase("A"))
.fetchCount();
ありがとう

このようなものを試してください-
QCategoryカテゴリ= QCategory.category;
QCategory subCategory = new QCategory( "sub");
query.from(category)
.leftJoin(category.subcategory、subCategory)
.where(category.type.equalsIgnoreCase( "A"))
.fetchCount();

これにより、subCategoryにエイリアスが与えられ、where句では、左側に結合されたエンティティではなく、メインエンティティが選択されます。

このようなものを試してください-
QCategoryカテゴリ= QCategory.category;
QCategory subCategory = new QCategory( "sub");
query.from(category)
.leftJoin(category.subcategory、subCategory)
.where(category.type.equalsIgnoreCase( "A"))
.fetchCount();

これにより、subCategoryにエイリアスが与えられ、where句では、左側に結合されたエンティティではなく、メインエンティティが選択されます。

なぜそれが機能するのかはわかりませんが、確かにうまくいきました。
どうもありがとう!
:)

このページは役に立ちましたか?
0 / 5 - 0 評価