Querydsl: Issue in fetch when selfJoin is in query

Created on 25 Apr 2019  ·  4Comments  ·  Source: querydsl/querydsl

I am having a case where I have self join in table.
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())
.fetch()
.stream()
.map(queryBase::mapRow)
.collect(Collectors.toList());

Alias of the QTodoEntity created on left join is used everywhere in the query. While it should use the alias which was created on select from.
Query looks like

select todo1.id, todo1.name, todo1.custom, todo1.selfChild from Todo todoOnSelect left outer join Todo todo1 on todoOnSelect.selfChild=todo1.id

but query should be -
select todoOnSelect.id, todoOnSelect.name, todoOnSelect.custom, todoOnSelect.selfChild from Todo todoOnSelect left outer join Todo todo1 on todoOnSelect.selfChild=todo1.id

Most helpful comment

Try something like this -
QCategory category = QCategory.category;
QCategory subCategory = new QCategory("sub");
query.from(category)
.leftJoin(category.subcategory, subCategory)
.where(category.type.equalsIgnoreCase("A"))
.fetchCount();

This will give an alias to subCategory and in where clause it will pick the main entity, not the left joined one.

All 4 comments

This is not a bug.

This is not a bug.

How did you resolve this? I'm having the same problem, the where clause is using the 'left join' alias, instead of the 'from' alias, and the results are incorrect.

I'm getting:
select count(sub.id) from category c
left join category sub on c.id = sub.id
where sub.type='A'

It should be:
select count(c.id) from category c
left join category sub on c.id = sub.id
where c.type='A'

Here's the queryDsl:
QCategory category = QCategory.category;
query.from(category)
.leftJoin(category.subcategory, QCategory.category)
.where(category.type.equalsIgnoreCase("A"))
.fetchCount();
Thanks

Try something like this -
QCategory category = QCategory.category;
QCategory subCategory = new QCategory("sub");
query.from(category)
.leftJoin(category.subcategory, subCategory)
.where(category.type.equalsIgnoreCase("A"))
.fetchCount();

This will give an alias to subCategory and in where clause it will pick the main entity, not the left joined one.

Try something like this -
QCategory category = QCategory.category;
QCategory subCategory = new QCategory("sub");
query.from(category)
.leftJoin(category.subcategory, subCategory)
.where(category.type.equalsIgnoreCase("A"))
.fetchCount();

This will give an alias to subCategory and in where clause it will pick the main entity, not the left joined one.

Not Sure why it works, but it sure did the trick.
Thank you so much!
:)

Was this page helpful?
0 / 5 - 0 ratings