使用 SUM 和 COUNT 的 JPQL 本机查询
我正在尝试运行以下代码:
public BigDecimal valuate(String searchTerms, String categoryPath) {
Query query = em.createNativeQuery("SELECT SUM(maxBidAmount) / COUNT(maxBidAmount) FROM Item WHERE MATCH(title) AGAINST(':searchTerms') AND categoryPath=':categoryPath'", Double.class);
query.setParameter("searchTerms", searchTerms);
query.setParameter("categoryPath", categoryPath);
double value = (double) query.getSingleResult();
return new BigDecimal(value);
}
当我这样做时,我得到以下异常:
Exception Description: Missing descriptor for [class java.lang.Double].
当我删除 Double.class
时,我得到一个不同的异常。
所以,我只是想知道将 COUNT 和 SUM 与 JPQL 结合使用的正确方法。
I am trying to run the following code:
public BigDecimal valuate(String searchTerms, String categoryPath) {
Query query = em.createNativeQuery("SELECT SUM(maxBidAmount) / COUNT(maxBidAmount) FROM Item WHERE MATCH(title) AGAINST(':searchTerms') AND categoryPath=':categoryPath'", Double.class);
query.setParameter("searchTerms", searchTerms);
query.setParameter("categoryPath", categoryPath);
double value = (double) query.getSingleResult();
return new BigDecimal(value);
}
When I do so, I get the following exception:
Exception Description: Missing descriptor for [class java.lang.Double].
When I remove Double.class
, I get a different exception.
So, I'm just wondering the correct method of using COUNT and SUM with JPQL.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果 SQL 有效,则无需在查询定义中指定 Double.class - 只需使用 em.createNativeQuery(SQLString);
当您希望 JPA 提供程序根据结果构建实体,但在本例中您需要原始数据时,将使用返回类型。
IF the SQL is valid, you do not need to specify the Double.class in the query def - just use em.createNativeQuery(SQLString);
The return type is used when you want the JPA provider to build an entity from the results, but in this case you want the raw data.
本机查询是SQL,而不是JPQL,并且您可以像使用 RDBMS 的任何 SQL 一样使用这些关键字。
看起来您的 JPA 提供程序不接受 Double 作为结果类(有些只允许结果类是实体)。
DataNucleus JPA 当然允许非实体结果类。
Native query is SQL, not JPQL, and you use those keywords just like any SQL for your RDBMS.
Looks like your JPA provider doesn't accept Double as a result class (some only allow the result class to be an Entity).
DataNucleus JPA certainly allows non-Entity result classes.