使用SUM和COUNT的JPQL本机查询 [英] JPQL Native Query using SUM and COUNT

查看:172
本文介绍了使用SUM和COUNT的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].

当我删除Double.class时,我得到了另一个异常.

When I remove Double.class, I get a different exception.

因此,我只是想知道将COUNT和SUM与JPQL一起使用的正确方法.

So, I'm just wondering the correct method of using COUNT and SUM with JPQL.

推荐答案

如果SQL有效,则无需在查询def中指定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.

这篇关于使用SUM和COUNT的JPQL本机查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆