论坛首页 Java企业应用论坛

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

浏览 13243 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2006-10-24  
终于发现问题了,配置文件问题.
当查询解析器配置为
 <property name="hibernate.query.factory_class">org.hibernate.hql.classic.ClassicQueryTranslatorFactory</property>

出错
配置为
 <property name="org.hibernate.hql.ast.ASTQueryTranslatorFactory"</property>

能正常运行.
怀疑第一个解析器有问题.
感谢:together、BirdGu的帮助!
0 请登录后投票
   发表时间:2006-10-24  
测试环境:MS Sql Server 2005 Express SP1; Hibernate 3.2.0 GA; Tomcat 5.5.20

我的愚钝的测试代码:(图省事,直接用Spring的HibernateTemplate.find()来测试。到了Hibernate都是session.createQuery(),应该差别不大……)
	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) | Valid
Hibernate:
    /*  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) | Valid
Hibernate:
    /*  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) | Valid
Hibernate:
    /*  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 上面得到的结果完全相同。
0 请登录后投票
   发表时间:2006-10-24  
ljj 写道
终于发现问题了,配置文件问题.
当查询解析器配置为
 <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,先生何苦专门自己指定一下呢?

不过这个问题我还是头一次听说,谢谢分享经验了哈。
0 请登录后投票
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics