[英]Group concat equivalent in queryDSL
What is the equivalent expression for a group concat SQL query in queryDSL? queryDSL中组concat SQL查询的等效表达式是什么? I am using queryDSL with spring JPA, and it is updated to the latest version, and I was not able to find such a thing while reading the docs.
我正在使用带有spring JPA的queryDSL,并且它已更新到最新版本,并且在阅读文档时我无法找到这样的东西。
Thanks. 谢谢。
I asked the developers to add the feature. 我让开发人员添加了这个功能。 The fix is committed here.
修复程序在此处提交。
https://github.com/querydsl/querydsl/commit/23e2471b4735ad50f5ac33ad539474d113bb5b07 https://github.com/querydsl/querydsl/commit/23e2471b4735ad50f5ac33ad539474d113bb5b07
and they even provided a test case in the commit, so here is an example: 他们甚至在提交中提供了一个测试用例,所以这里有一个例子:
@Test
@ExcludeIn(DERBY)
public void groupConcat() {
assertEquals(
ImmutableList.of("Mike,Mary", "Joe,Peter,Steve,Jim", "Jennifer,Helen,Daisy,Barbara"),
query().select(SQLExpressions.groupConcat(employee.firstname))
.from(employee).orderBy(groupConcatOrder())
.groupBy(employee.superiorId).fetch());
}
@Test
@ExcludeIn(DERBY)
public void groupConcat2() {
assertEquals(
ImmutableList.of("Mike-Mary", "Joe-Peter-Steve-Jim", "Jennifer-Helen-Daisy-Barbara"),
query().select(SQLExpressions.groupConcat(employee.firstname, "-"))
.from(employee).orderBy(groupConcatOrder())
.groupBy(employee.superiorId).fetch());
}
private OrderSpecifier<?>[] groupConcatOrder() {
if (Connections.getTarget() == Target.POSTGRESQL) {
return new OrderSpecifier[] {employee.id.asc()};
} else {
return new OrderSpecifier<?>[0];
}
}
I found the answer by myself. 我自己找到了答案。 It can be done using the transform function in queryDSL
可以使用queryDSL中的transform函数完成
From http://www.querydsl.com/static/querydsl/3.4.3/reference/html/ch03s02.html Section 3.2.4 来自http://www.querydsl.com/static/querydsl/3.4.3/reference/html/ch03s02.html第3.2.4节
Map<Integer, List<Comment>> results = query.from(post, comment)
.where(comment.post.id.eq(post.id))
.transform(groupBy(post.id).as(list(comment)));
So, for example, if you want group by on 2 columns, the following code will work: 因此,例如,如果您想在2列上分组,则以下代码将起作用:
Map<List<?>, List<Comment>> results = query.from(post, comment)
.where(comment.post.id.eq(post.id))
.transform(groupBy(post.id, post.hooplah).as(list(comment)));
which produces a map of List_of_groupByFieldValues --> List_of_results. 它生成List_of_groupByFieldValues - > List_of_results的映射。
Can someone confirm if this is the standard way to do a group concat in queryDSL? 有人可以确认这是否是在queryDSL中进行组连接的标准方法?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.