Hibernate namedquery 抛出 ORA-00979: not a GROUP BY 表达式

Posted

技术标签:

【中文标题】Hibernate namedquery 抛出 ORA-00979: not a GROUP BY 表达式【英文标题】:Hibernate namedquery throws ORA-00979: not a GROUP BY expression 【发布时间】:2019-06-13 10:56:58 【问题描述】:

我有一个关于从基于休眠的命名查询中获取结果的问题。

这里是命名查询。

@NamedQuery(name = "DetailOrder.bestSelling", 
    query = "SELECT do.book FROM DetailOrder do GROUP BY do.book.id "
                    + "ORDER BY SUM(do.quantity) DESC")


@NamedQuery(name = "Review.mostFavoredBooks",
    query = "SELECT r.bookReview, COUNT(r.bookReview.id) AS ReviewCount, AVG(r.rating) as AvgRating FROM Review r "
                        + "GROUP BY r.bookReview.id HAVING AVG(r.rating) >= 4.0 "
                        + "ORDER BY ReviewCount DESC, AvgRating DESC")

值和别名正确。

这里是命名查询相关的函数。

public List<Book> listMostFavoredBooks() 
        List<Book> mostFavoredBooks = new ArrayList<>();

        List<Object[]> result = super.findByNameQueryObjects("Review.mostFavoredBooks", 0, 4);

        System.out.println("listMostFavoredBooks | result size :" + result.size());

        if (!result.isEmpty()) 
            for (Object[] elements : result) 
                Book book = (Book) elements[0];
                mostFavoredBooks.add(book);
            
         

        return mostFavoredBooks;
    

public List<Book> listBestSellingBooks() 
        return super.findByNameQuery("DetailOrder.bestSelling", 0, 4);
   

当我从上面定义的命名查询中得到结果时,所有这些都存在相同的问题。

Caused by: java.sql.SQLSyntaxErrorException: ORA-00979: not a GROUP BY expression

我该如何解决?

【问题讨论】:

ORA-00979 not a group by expression的可能重复 @XtremeBaumer 如何更改查询?不介意的话可以写吗? 您可以编写两个独立的查询,一个用于计数,另一个用于平均。您应该阅读上面链接中的答案。 “您必须将 SELECT 的所有列放在 GROUP BY 中,或者在它们上使用函数将结果压缩为 单个 值(如 MIN、MAX 或 SUM)。” @RDK 我编辑帖子。 哪个查询有误?关于第二个是什么的一些解释? 【参考方案1】:

如果这个选择:

SELECT do.book 
FROM DetailOrder do 
GROUP BY do.book.id 
ORDER BY SUM(do.quantity) DESC

是Oracle sql 不正确。 select 和 group by 表达式必须匹配。

我认为“查询”部分是这样的:

@NamedQuery(name = "DetailOrder.bestSelling", 
query = "SELECT do.book FROM DetailOrder do GROUP BY do.book.id "
                + "ORDER BY SUM(do.quantity) DESC")

其实是一个真正的sql语句,而不是hibernate语句。

【讨论】:

我也写了@NamedQuery(name = "DetailOrder.bestSelling", query = "SELECT do.book FROM DetailOrder do GROUP BY do.book" + "ORDER BY SUM(do.quantity) DESC" ) 但没有任何改变。 这是不正确的,正确的查询应该是:“select do.book.sum(do.quantity) from DetailOrder do GROUP BY do.book order by 2 desc”。这应该直接在 oracle 中工作【参考方案2】:

我的答案

SELECT do.book_ıd,SUM(do.quantity) FROM detaılorder do LEFT JOIN book b on do.book_ıd = b.book_ıd
GROUP BY do.book_ıd
ORDER BY SUM(do.quantity) DESC;

SELECT r.book_ıd,COUNT(r.book_ıd) AS ReviewCount,AVG(r.rating) as AvgRating FROM revıew r LEFT JOIN book b on r.book_ıd = b.book_ıd
GROUP BY r.book_ıd HAVING AVG(r.rating) >= 4.0
ORDER BY ReviewCount DESC, AvgRating DESC;

【讨论】:

以上是关于Hibernate namedquery 抛出 ORA-00979: not a GROUP BY 表达式的主要内容,如果未能解决你的问题,请参考以下文章

在 Hibernate 的 NamedQuery 中使用用户定义的变量

将@NamedQuery 用于单列选择(J2EE/JPA、Hibernate)是一种好习惯吗?

在 HashMap 上具有内部联接的 HQL NamedQuery

Hibernate用oracle生成无效的sql

使用@namedquery 更新查询无法正常工作

JPA 中所有 NamedQueries 的列表