我需要用 sum 执行一个 HQL 查询
,然后 java 中的结束子句在 postgres
中运行没有问题:
SELECT r.reviewer_id,
sum(case when c.service_type = 'مالتی مدیا' AND c.genre_id = '66c92962-324c-11e7-93ae-92361f002671' then 1 else 0 end) image_multimedia,
sum(case when c.service_type is null AND c.genre_id = '66c92b1a-324c-11e7-93ae-92361f002671' then 1 else 0 end) image_null
FROM module_samim.content c
Join module_samim.content_reviewer r on c.id = r.content_id
Join module_samim.file f on f.id = c.file_id
Group by r.reviewer_id
我试图用< code>createQuery()运行它,但是我得到:
Sep 11, 2018 8:57:20 AM org.hibernate.hql.internal.ast.ErrorCounter reportError
ERROR: line 1:58: expecting "then", found 'مدیاAND'
line 1:58: expecting "then", found 'مدیاAND'
and
Sep 11, 2018 8:57:20 AM org.hibernate.hql.internal.ast.ErrorCounter reportError
ERROR: line 1:264: unexpected token: image_null
Sep 11, 2018 8:57:20 AM org.hibernate.hql.internal.ast.ErrorCounter reportError
ERROR: line 1:264: unexpected token: image_null
line 1:264: unexpected token: image_null
请帮帮我!谢谢大家!
语法似乎是SQL,而不是HQL。
因此,我们需要的不是这个
createQuery()
称之为
createSqlQuery()
我找到了解决方案:))我使用createNativeQuery()
和@SqlResultSetMapping在这个链接中解释了这一点,以完全解决我的问题
看起来你把原生查询和HQL/JPQL createQuery API混合使用了
为了向您澄清这一点,我们有三种不同的方法可以在 Hibernate 中创建 SQL 查询:
1) session.createQuery() //Hibernate APi
2) entityManager.createQuery() //JPA Api
3) session.createSQLQuery() // **** Hibernate API ****
4) entityManager.createNativeQuery() //JPA API