Querydsl: 查询 selfJoin 时获取问题

创建于 2019-04-25  ·  4评论  ·  资料来源: querydsl/querydsl

我有一个案例,我在表中加入了自我。
查询工厂
.select(queryBase.getColumns())
.from(实体路径)
.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(类别)
.leftJoin(category.subcategory, subCategory)
.where(category.type.equalsIgnoreCase("A"))
.fetchCount();

这将为 subCategory 提供一个别名,并且在 where 子句中它将选择主实体,而不是左连接实体。

所有4条评论

这不是错误。

这不是错误。

你是如何解决这个问题的? 我遇到了同样的问题,where 子句使用的是“left join”别名,而不是“from”别名,结果不正确。

我越来越:
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'

这是查询Dsl:
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(类别)
.leftJoin(category.subcategory, subCategory)
.where(category.type.equalsIgnoreCase("A"))
.fetchCount();

这将为 subCategory 提供一个别名,并且在 where 子句中它将选择主实体,而不是左连接实体。

尝试这样的事情 -
QCategory 类别 = QCategory.category;
QCategory subCategory = new QCategory("sub");
query.from(类别)
.leftJoin(category.subcategory, subCategory)
.where(category.type.equalsIgnoreCase("A"))
.fetchCount();

这将为 subCategory 提供一个别名,并且在 where 子句中它将选择主实体,而不是左连接实体。

不知道为什么它有效,但它确实做到了。
非常感谢!
:)

此页面是否有帮助?
0 / 5 - 0 等级