使用 SUM 和 COUNT 的 JPQL 本机查询

发布于 2024-12-28 16:13:48 字数 723 浏览 3 评论 0原文

我正在尝试运行以下代码:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

心头的小情儿 2025-01-04 16:13:48

如果 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.

め可乐爱微笑 2025-01-04 16:13:48

本机查询是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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文