我尝试使用querydsl-collections,但未能编写简单的groupBy表达式。这是我的尝试:
import static com.mysema.query.alias.Alias.$;
import static com.mysema.query.alias.Alias.alias;
import static com.mysema.query.collections.CollQueryFactory.from;
import static java.util.Arrays.asList;
import java.util.List;
import com.mysema.query.Tuple;
public class QuerydslCollectionsGroupByTest {
public static void main(String[] args) {
List<Foo> foos = asList(
new Foo("a1", 1),
new Foo("a1", 2),
new Foo("a1", 3),
new Foo("a2", 4),
new Foo("a2", 5),
new Foo("a2", 6)
);
Foo f = alias(Foo.class, "foo");
List<Tuple> result = from($(f), foos)
.groupBy($(f.getA()))
.list($(f.getA()), $(f.getSum()).sum());
System.out.println(result);
}
public static class Foo {
private String a;
private Integer sum;
public Foo() {
}
public Foo(String a, Integer sum) {
this.a = a;
this.sum = sum;
}
public String getA() {
return a;
}
public Integer getSum() {
return sum;
}
public void setSum(Integer sum) {
this.sum = sum;
}
public void setA(String a) {
this.a = a;
}
}
}
但是当我尝试运行它时。我明白了:
Exception in thread "main" com.mysema.codegen.CodegenException: Compilation of public class Q_838101820_201273267_01405199074_997653153 {
public static com.mysema.query.Tuple eval(com.sopovs.moradanen.zkoss.QuerydslCollectionsGroupByTest.Foo foo, com.mysema.query.types.QTuple a1) {
return (com.mysema.query.Tuple)(a1.newInstance(foo.getA(), sum(foo.getSum()))); }
}
failed.
/Q_838101820_201273267_01405199074_997653153.java:4: error: cannot find symbol
return (com.mysema.query.Tuple)(a1.newInstance(foo.getA(), sum(foo.getSum()))); }
^
symbol: method sum(Integer)
location: class Q_838101820_201273267_01405199074_997653153
1 error
at com.mysema.codegen.JDKEvaluatorFactory.compile(JDKEvaluatorFactory.java:74)
at com.mysema.codegen.AbstractEvaluatorFactory.createEvaluator(AbstractEvaluatorFactory.java:128)
at com.mysema.codegen.AbstractEvaluatorFactory.createEvaluator(AbstractEvaluatorFactory.java:94)
at com.mysema.query.collections.DefaultEvaluatorFactory.create(DefaultEvaluatorFactory.java:125)
at com.mysema.query.collections.DefaultQueryEngine.project(DefaultQueryEngine.java:234)
at com.mysema.query.collections.DefaultQueryEngine.evaluateSingleSource(DefaultQueryEngine.java:192)
at com.mysema.query.collections.DefaultQueryEngine.list(DefaultQueryEngine.java:91)
at com.mysema.query.collections.AbstractCollQuery.list(AbstractCollQuery.java:202)
at com.mysema.query.collections.AbstractCollQuery.list(AbstractCollQuery.java:195)
at com.sopovs.moradanen.zkoss.QuerydslCollectionsGroupByTest.main(QuerydslCollectionsGroupByTest.java:27)
这是一个错误吗?