我在原生查询中遇到了一件奇怪的事情:
如果我尝试使用如下命名的本机查询
@NamedNativeQuery(name = "native_count_avg_guest_quizzes", resultClass = java.math.BigDecimal.class, query = "select avg(c) as r from ((select count(*) as c from user_quiz uq join user u on (uq.user_id = u.id) where u.status = 'temporary' group by u.id) as res)")
该应用程序无法运行,我有
org.hibernate.HibernateException: Errors in named queries: native_count_avg_guest_quizzes_
但是,如果我不使用 NamedNativeQuery 并且仅创建如下动态本机查询,则相同的查询可以正常工作:
entityManager.createNativeQuery(
"select avg(c) as r from ((select count(*) as c from user_quiz uq join user u on (uq.user_id = u.id) where u.status = 'temporary' group by u.id) as res)")
.getSingleResult()
为什么?我对 NamedNativeQuery 做错了什么?谢谢
更新: 实体类如下
@Entity
@Table(name = "user_quiz")
@NamedNativeQueries({
@NamedNativeQuery(name = "native_count_avg_guest_quizzes", resultClass = java.math.BigDecimal.class, query = "select avg(c) as r from ((select count(*) as c from user_quiz uq join user u on (uq.user_id = u.id) where u.status = 'temporary' group by u.id) as res)")
})
@NamedQueries({
@NamedQuery(name = "list_clients_quizzes", query = "select uq from UserQuiz uq where uq.quiz.client.id = :clientId"),
.......
})
public class UserQuiz extends Measurable {
.......
}