为了账号安全,请及时绑定邮箱和手机立即绑定

hibernate hql sum case when else end execution in

hibernate hql sum case when else end execution in

江户川乱折腾 2021-09-12 19:48:38
我需要HQL query在Java中执行with sum when else end 子句,该子句在以下环境中运行没有问题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我试图运行它,createQuery()但我得到:Sep 11, 2018 8:57:20 AM org.hibernate.hql.internal.ast.ErrorCounter reportErrorERROR: line 1:58: expecting "then", found 'مدیاAND'line 1:58: expecting "then", found 'مدیاAND'andSep 11, 2018 8:57:20 AM org.hibernate.hql.internal.ast.ErrorCounter reportErrorERROR: line 1:264: unexpected token: image_nullSep 11, 2018 8:57:20 AM org.hibernate.hql.internal.ast.ErrorCounter reportErrorERROR: line 1:264: unexpected token: image_nullline 1:264: unexpected token: image_null请帮我!谢谢你!
查看完整描述

3 回答

?
炎炎设计

TA贡献1808条经验 获得超4个赞

语法似乎是 SQL,而不是 HQL。

因此,我们需要代替这个

createQuery()

称之为

createSqlQuery()


查看完整回答
反对 回复 2021-09-12
?
郎朗坤

TA贡献1921条经验 获得超9个赞

我找到了解决方案:)) 我使用createNativeQuery()并在此链接中解释了@SqlResultSetMapping以完全解决我的问题


查看完整回答
反对 回复 2021-09-12
?
拉风的咖菲猫

TA贡献1995条经验 获得超2个赞

您似乎在 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

因此,如果您使用 Hibernate,您应该使用 session.createSQLQuery(),如果您使用 JPA,您应该使用 entityManager.createNativeQuery()


查看完整回答
反对 回复 2021-09-12
  • 3 回答
  • 0 关注
  • 173 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号