PL/SQL 的SQL语句可以执行,但是放在hibernate中,后台打印,出现了错误。
错误的SQL解析:黄色为错误部分
Hibernate:
select
examinee0_.EXAM_YEAR as col_0_0_,
count(*) as col_1_0_,
sum(caseexaminee0_.CHECK_FLAGwhen'2'then1else 0end) as col_2_0_
from
vet_test.EXAMINEE examinee0_
group by
examinee0_.EXAM_YEAR
order by
examinee0_.EXAM_YEAR ASC
很明显 ,标黄的地方出错,空格不翼而飞,后台提示ORA-00907: 缺失右括号
经过一番检查!终于发现了问题所在:那就是hibernate的SQL查询翻译器!
Hibernate3.0 采用新的基于ANTLR的HQL/SQL查询翻译器,
在hibernate的配置文件中,hibernate.query.factory_class
属性用来选择查询翻译器。
(1)选择Hibernate3.0的查询翻译器:
hibernate.query.factory_class=
org.hibernate.hql.ast.ASTQueryTranslatorFactory
(2)选择Hibernate2.1的查询翻译器hibernate.query.factory_class=
org.hibernate.hql.classic.ClassicQueryTranslatorFactory
解决方案:
将hibernate.cfg.xml的
<property name="hibernate.query.factory_class">
org.hibernate.hql.classic.ClassicQueryTranslatorFactory
</property>
标蓝色的部分替换成org.hibernate.hql.ast.ASTQueryTranslatorFactory
替换后后台打印解析后的sql:黄色为解析正确后的sql
Hibernate:
select
examinee0_.EXAM_YEAR as col_0_0_,
count(*) as col_1_0_,
sum(case examinee0_.CHECK_FLAG
when '2' then 1
else 0
end) as col_2_0_
from
vet_test.EXAMINEE examinee0_
group by
examinee0_.EXAM_YEAR
order by
examinee0_.EXAM_YEAR ASC