【问题标题】:Select Long type variable in Querydsl在Querydsl中选择Long类型变量
【发布时间】:2016-04-28 15:24:54
【问题描述】:

当我尝试运行此查询时:

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

我得到编译错误,因为构造函数在变量count 处期望NumberPath&lt;Long&gt; 而不是Long,那么如何在querydsl 中选择一个变量?

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

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

但我得到了这个执行

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

【问题讨论】:

  • 请提供CritereItem的代码

标签: hibernate jpa querydsl


【解决方案1】:

您正试图通过 QueryDSL 构造函数传递一个常量。

查看 Expressions 静态类以获取更多信息: QueryDSL API Reference

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

另外请注意,如果您想汇总计数,可以使用foo.count() 来完成

【讨论】:

  • 函数asNumber() 未为Expressions 类型定义
  • 您使用的是哪个版本的 QueryDSL?尝试更新到最新版本。
  • 我用的是3.4.1版本
  • 对于 3.4.1,试试 Expressions.numberTemplate(Long.class, ...) 。第二个参数必须是一个字符串,你可能需要使用Long.toString(...)
  • 我收到了这个异常java.lang.IllegalArgumentException: java.lang.ClassCastException@14edf4
猜你喜欢
  • 1970-01-01
  • 2020-11-10
  • 1970-01-01
  • 2021-01-20
  • 1970-01-01
  • 2019-04-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多