首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 软件管理 > 软件架构设计 >

hibernate 难道不支持case when then end 吗?[已解决,多谢关注]

2012-11-08 
hibernate 难道不支持case when then end 吗?[已解决,谢谢关注]hibernate 版本:网上最新的hibernate-3.2.0

hibernate 难道不支持case when then end 吗?[已解决,谢谢关注]
hibernate 版本:网上最新的hibernate-3.2.0.ga
执行hql:
select case when c.id>'1' then '111' else '333' END from QuCodeRegion c
报错:
QueryException: undefined alias: case [select case when c.id>'1' then '111' else '333' END from QuCodeRegion c]

如果将case 放到where语名后面,则一切正常.
select c.id  from QuCodeRegion c where case when c.id>'1' then '111' else '333' END='111'
一切正常.
这是为什么,难道hibernate 不支持这种语法.
但hibernate的测试用例里明明有这样的程序(一部分代码):


// $Id: ASTParserLoadingTest.java 9531 2006-03-02 03:14:31Z steve.ebersole@jboss.com $package org.hibernate.test.hql;public class ASTParserLoadingTest extends TestCase {public ASTParserLoadingTest(String name) {super( name );}public void testSelectClauseCase() {Session s = openSession();Transaction t = s.beginTransaction();Human h = new Human();h.setBodyWeight( (float) 74.0 );h.setHeight(120.5);h.setDescription("Me");h.setName( new Name("Gavin", 'A', "King") );h.setNickName("Oney");s.persist(h);String name = (String) s.createQuery("select case nickName when 'Oney' then 'gavin' when 'Turin' then 'christian' else nickName end from Human").uniqueResult();assertEquals(name, "gavin");String result = (String) s.createQuery("select case when bodyWeight > 100 then 'fat' else 'skinny' end from Human").uniqueResult();assertEquals(result, "skinny");s.delete(h);t.commit();s.close();}}


解决办法:
终于发现问题了,配置文件问题.
当查询解析器配置为


代码

<property name="hibernate.query.factory_class">org.hibernate.hql.classic.ClassicQueryTranslatorFactory</property>


出错
配置为

代码

<property name="org.hibernate.hql.ast.ASTQueryTranslatorFactory"</property>


能正常运行.
怀疑第一个解析器有问题.
感谢:together、BirdGu的帮助!Exception in thread "main" org.hibernate.QueryException: undefined alias: case [select case when codeid>'1' then '111' else '333' END from QuCodeRegion]at org.hibernate.hql.classic.PathExpressionParser.token(PathExpressionParser.java:130)at org.hibernate.hql.classic.ParserHelper.parse(ParserHelper.java:28)at org.hibernate.hql.classic.SelectParser.token(SelectParser.java:176)at org.hibernate.hql.classic.ClauseParser.token(ClauseParser.java:86)at org.hibernate.hql.classic.ClauseParser.end(ClauseParser.java:113)at org.hibernate.hql.classic.PreprocessingParser.end(PreprocessingParser.java:122)at org.hibernate.hql.classic.ParserHelper.parse(ParserHelper.java:29)at org.hibernate.hql.classic.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:192)at org.hibernate.hql.classic.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:168)at org.hibernate.engine.query.HQLQueryPlan.<init>(HQLQueryPlan.java:77)at org.hibernate.engine.query.HQLQueryPlan.<init>(HQLQueryPlan.java:56)at org.hibernate.engine.query.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:72)at org.hibernate.impl.AbstractSessionImpl.getHQLQueryPlan(AbstractSessionImpl.java:133)at org.hibernate.impl.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:112)at org.hibernate.impl.SessionImpl.createQuery(SessionImpl.java:1623)at cn.com.cfit.gztfjg.commonMode.BaseDAO.findObj(BaseDAO.java:208)at test3.TestCase.main(TestCase.java:13)select case when price > 100 then 'high' else 'low' END from Book <property name="hibernate.query.factory_class">org.hibernate.hql.classic.ClassicQueryTranslatorFactory</property>
出错
配置为
 <property name="org.hibernate.hql.ast.ASTQueryTranslatorFactory"</property>

能正常运行.
怀疑第一个解析器有问题.
感谢:together、BirdGu的帮助!String queryStr1 = " select case when m.flag = 1 then 'Valid' else 'Invalid' END from Mobilization m ";String queryStr2 = " select case when flag = 1 then 'Valid' else 'Invalid' END from Mobilization ";String queryStr3 = " select case flag when 1 then 'Valid' else 'Invalid' END from Mobilization ";String queryStr4 = " select case m.flag when 1 then 'Valid' else 'Invalid' END from Mobilization m ";List list1 = getHibernateTemplate().find(queryStr1);for (Iterator it = list1.iterator(); it.hasNext(); ) {log.debug(it.next());}List list2 = getHibernateTemplate().find(queryStr2);for (Iterator it = list2.iterator(); it.hasNext(); ) {log.debug(it.next());}List list3 = getHibernateTemplate().find(queryStr3);for (Iterator it = list3.iterator(); it.hasNext(); ) {log.debug(it.next());}List list4 = getHibernateTemplate().find(queryStr4);for (Iterator it = list4.iterator(); it.hasNext(); ) {log.debug(it.next());}

控制台的输出:
Hibernate:    /*  select        case            when m.flag = 1 then 'Valid'            else 'Invalid'        END    from        Mobilization m  */ select            case                when mobilizati0_.flag=1 then 'Valid'                else 'Invalid'            end as col_0_0_        from            Mobilization mobilizati0_[nosense] DEBUG MobilizationHqlCaseTest.getMobByConditions(255) | Invalid[nosense] DEBUG MobilizationHqlCaseTest.getMobByConditions(255) | Invalid[nosense] DEBUG MobilizationHqlCaseTest.getMobByConditions(255) | Invalid[nosense] DEBUG MobilizationHqlCaseTest.getMobByConditions(255) | Invalid[nosense] DEBUG MobilizationHqlCaseTest.getMobByConditions(255) | Invalid[nosense] DEBUG MobilizationHqlCaseTest.getMobByConditions(255) | Valid[nosense] DEBUG MobilizationHqlCaseTest.getMobByConditions(255) | Valid[nosense] DEBUG MobilizationHqlCaseTest.getMobByConditions(255) | Valid[nosense] DEBUG MobilizationHqlCaseTest.getMobByConditions(255) | Valid[nosense] DEBUG MobilizationHqlCaseTest.getMobByConditions(255) | Valid[nosense] DEBUG MobilizationHqlCaseTest.getMobByConditions(255) | ValidHibernate:    /*  select        case            when flag = 1 then 'Valid'            else 'Invalid'        END    from        Mobilization  */ select            case                when mobilizati0_.flag=1 then 'Valid'                else 'Invalid'            end as col_0_0_        from            Mobilization mobilizati0_[nosense] DEBUG MobilizationHqlCaseTest.getMobByConditions(260) | Invalid[nosense] DEBUG MobilizationHqlCaseTest.getMobByConditions(260) | Invalid[nosense] DEBUG MobilizationHqlCaseTest.getMobByConditions(260) | Invalid[nosense] DEBUG MobilizationHqlCaseTest.getMobByConditions(260) | Invalid[nosense] DEBUG MobilizationHqlCaseTest.getMobByConditions(260) | Invalid[nosense] DEBUG MobilizationHqlCaseTest.getMobByConditions(260) | Valid[nosense] DEBUG MobilizationHqlCaseTest.getMobByConditions(260) | Valid[nosense] DEBUG MobilizationHqlCaseTest.getMobByConditions(260) | Valid[nosense] DEBUG MobilizationHqlCaseTest.getMobByConditions(260) | Valid[nosense] DEBUG MobilizationHqlCaseTest.getMobByConditions(260) | Valid[nosense] DEBUG MobilizationHqlCaseTest.getMobByConditions(260) | ValidHibernate:    /*  select        case flag            when 1 then 'Valid'            else 'Invalid'        END    from        Mobilization  */ select            case mobilizati0_.flag                when 1 then 'Valid'                else 'Invalid'            end as col_0_0_        from            Mobilization mobilizati0_[nosense] DEBUG MobilizationHqlCaseTest.getMobByConditions(265) | Invalid[nosense] DEBUG MobilizationHqlCaseTest.getMobByConditions(265) | Invalid[nosense] DEBUG MobilizationHqlCaseTest.getMobByConditions(265) | Invalid[nosense] DEBUG MobilizationHqlCaseTest.getMobByConditions(265) | Invalid[nosense] DEBUG MobilizationHqlCaseTest.getMobByConditions(265) | Invalid[nosense] DEBUG MobilizationHqlCaseTest.getMobByConditions(265) | Valid[nosense] DEBUG MobilizationHqlCaseTest.getMobByConditions(265) | Valid[nosense] DEBUG MobilizationHqlCaseTest.getMobByConditions(265) | Valid[nosense] DEBUG MobilizationHqlCaseTest.getMobByConditions(265) | Valid[nosense] DEBUG MobilizationHqlCaseTest.getMobByConditions(265) | Valid[nosense] DEBUG MobilizationHqlCaseTest.getMobByConditions(265) | ValidHibernate:    /*  select        case m.flag            when 1 then 'Valid'            else 'Invalid'        END    from        Mobilization m  */ select            case mobilizati0_.flag                when 1 then 'Valid'                else 'Invalid'            end as col_0_0_        from            Mobilization mobilizati0_[nosense] DEBUG MobilizationHqlCaseTest.getMobByConditions(270) | Invalid[nosense] DEBUG MobilizationHqlCaseTest.getMobByConditions(270) | Invalid[nosense] DEBUG MobilizationHqlCaseTest.getMobByConditions(270) | Invalid[nosense] DEBUG MobilizationHqlCaseTest.getMobByConditions(270) | Invalid[nosense] DEBUG MobilizationHqlCaseTest.getMobByConditions(270) | Invalid[nosense] DEBUG MobilizationHqlCaseTest.getMobByConditions(270) | Valid[nosense] DEBUG MobilizationHqlCaseTest.getMobByConditions(270) | Valid[nosense] DEBUG MobilizationHqlCaseTest.getMobByConditions(270) | Valid[nosense] DEBUG MobilizationHqlCaseTest.getMobByConditions(270) | Valid[nosense] DEBUG MobilizationHqlCaseTest.getMobByConditions(270) | Valid[nosense] DEBUG MobilizationHqlCaseTest.getMobByConditions(270) | Valid


目前我的一点结论:
1. 有没有化名(alias)最后生成的SQL语句都是相同的;
2. 暂时没有发现如楼主所说的问题……

注:以上测试在 Hibernate 3.1.3 上面得到的结果完全相同。 <property name="hibernate.query.factory_class">org.hibernate.hql.classic.ClassicQueryTranslatorFactory</property>
出错
配置为
 <property name="org.hibernate.hql.ast.ASTQueryTranslatorFactory"</property>

能正常运行.
怀疑第一个解析器有问题.
感谢:together、BirdGu的帮助!
T_T Hibernate默认用的就是AST,先生何苦专门自己指定一下呢?

不过这个问题我还是头一次听说,谢谢分享经验了哈。

热点排行