Hibernate namedquery 抛出 ORA-00979: 不是 GROUP BY 表达式
Hibernate namedquery throws ORA-00979: not a GROUP BY expression
我在从基于休眠的 namedquery 获取结果时遇到问题。
这里是命名查询。
@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")
值和别名正确。
下面是namedqueries相关的函数。
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
我该如何解决?
如果这个select:
SELECT do.book
FROM DetailOrder do
GROUP BY do.book.id
ORDER BY SUM(do.quantity) DESC
是Oracle sql不正确。 select 和 group by 表达式必须匹配。
我认为 "query" 部分:
@NamedQuery(name = "DetailOrder.bestSelling",
query = "SELECT do.book FROM DetailOrder do GROUP BY do.book.id "
+ "ORDER BY SUM(do.quantity) DESC")
实际上是一个真正的 sql 语句,而不是休眠语句。
我的回答
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;
我在从基于休眠的 namedquery 获取结果时遇到问题。
这里是命名查询。
@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")
值和别名正确。
下面是namedqueries相关的函数。
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
我该如何解决?
如果这个select:
SELECT do.book
FROM DetailOrder do
GROUP BY do.book.id
ORDER BY SUM(do.quantity) DESC
是Oracle sql不正确。 select 和 group by 表达式必须匹配。
我认为 "query" 部分:
@NamedQuery(name = "DetailOrder.bestSelling",
query = "SELECT do.book FROM DetailOrder do GROUP BY do.book.id "
+ "ORDER BY SUM(do.quantity) DESC")
实际上是一个真正的 sql 语句,而不是休眠语句。
我的回答
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;