在“查询”中选择“长型变量”



当我尝试运行此查询时:

Long count = ...;
List<CritereItem> items= new JPAQuery(entityManager).from(foo)
                     .list( new QCritereItem( foo.id, foo.name, count  ));

我得到编译错误是因为构造函数期望NumberPath<Long>在变量countLong,那么如何在querydsl中选择一个变量呢?

我将构造函数中的计数替换为:

Expressions.numberTemplate(Long.class, count.toString())

但我得到了这个解释

java.lang.IllegalArgumentException: java.lang.ClassCastException@14edf4

您正在尝试通过 QueryDSL 构造函数传递常量。

检查表达式静态类以获取更多信息:查询DSL API 参考

NumberExpression<Long> count = Expressions.asNumber(...);
List<CritereItem> items = new JPAQuery(entityManager).from(foo)
                          .list( new QCritereItem( foo.id, foo.name, count ));

另请注意,如果要聚合计数,可以使用foo.count()

最新更新