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