`
ljj
  • 浏览: 13449 次
  • 来自: ...
社区版块
存档分类
最新评论

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的帮助!
分享到:
评论
12 楼 Allen 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,先生何苦专门自己指定一下呢?

不过这个问题我还是头一次听说,谢谢分享经验了哈。
11 楼 Allen 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 上面得到的结果完全相同。
10 楼 ljj 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的帮助!
9 楼 ljj 2006-10-24  
郁闷啊,折腾半天了.
8 楼 BirdGu 2006-10-24  
以下HQL在Hibernate 3.2下能正确执行。

select case when price > 100 then 'high' else 'low' END from Book
7 楼 together 2006-10-24  
嗯,我在用hb2的时候,倒是报的错误和你贴出来的一样。
我的查询方式是query = session.createQuery("select case when c.id>2 then '222' else '333' end from User c");
再报错,只能怪RPWT了。
6 楼 ljj 2006-10-24  
together 写道
我只在使用hibernate2的时候,才发现有这样的错误。
hibernate3.1,3.2都用过了,没问题。

你还是再确认一下版本吧。确认一下引用的可是org.hibernate?


版本应该没问题,以下是完整的错误信息:
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)
5 楼 together 2006-10-24  
我只在使用hibernate2的时候,才发现有这样的错误。
hibernate3.1,3.2都用过了,没问题。

你还是再确认一下版本吧。确认一下引用的可是org.hibernate?
4 楼 ljj 2006-10-24  
together 写道
和是否使用alias倒没什么关系
请说明你所使用的hibernate版本。
在hibernate2中,是不支持在select中的case when语句的。
在3.1中是完全可以执行的。

请问你用过吗?在错误提示里,将case,做为alias了.
3 楼 ljj 2006-10-24  
together 写道
和是否使用alias倒没什么关系
请说明你所使用的hibernate版本。
在hibernate2中,是不支持在select中的case when语句的。
在3.1中是完全可以执行的。
hibernate 版本:网上最新的hibernate-3.2.0.ga
2 楼 together 2006-10-24  
和是否使用alias倒没什么关系
请说明你所使用的hibernate版本。
在hibernate2中,是不支持在select中的case when语句的。
在3.1中是完全可以执行的。
1 楼 Allen 2006-10-24  
引用
select case nickName when 'Oney' then 'gavin' when 'Turin' then 'christian' else nickName end from Human

并没有使用alias...

而在
引用
select c.id from QuCodeRegion c where case when c.id>'1' then '111' else '333' END='111'

中,"c"是"QuCodeRegion"的alias...

相关推荐

    hibernate-shards.jar

    sections when you distribute them as separate works. But when you distribute the same sections as part of a whole which is a work based on the Library, the distribution of the whole must be on the ...

    hibernate_anoatation(中文版)

    @Formula("CASE WHEN gender = 'F' THEN 'Ms.' ELSE 'Mr.' END") private String title; ``` #### @Version - **用途**:用于实现乐观锁机制。 - **示例**: ```java @Version private int version; ``` #...

    hibernate所用到HQL经典语句大全

    String hql = "SELECT CASE WHEN c.age &gt; 5 THEN 'old' ELSE 'young' END AS ageDesc FROM Cat c"; List, Object&gt;&gt; list = session.createQuery(hql).list(); ``` 使用`CASE WHEN...THEN...ELSE...END`可以在...

    DB2、ORACLE

    - Oracle的DECODE函数在DB2中不支持,可以使用CASE WHEN语句替代,如`SELECT CASE WHEN f_areaid IS NULL THEN '空' ELSE f_areaid END FROM masa_user`。 7. **NVL函数**: - Oracle的NVL在DB2中需要使用...

    Java面试经典题

    SELECT CASE WHEN A &gt; B THEN A ELSE B END AS MaxCol FROM table; ``` - 当 B 列大于 C 列时选择 B 列,否则选择 C 列: ```sql SELECT CASE WHEN B &gt; C THEN B ELSE C END AS MaxCol FROM table; ``` #### ...

    Java程序员面试题大全

    - 统计x字段不同情况的个数,以及输出条数大于200的情况:`SELECT COUNT(DISTINCT x) AS distinct_count, SUM(CASE WHEN count(x) &gt; 200 THEN 1 ELSE 0 END) AS more_than_200 FROM (SELECT x, COUNT(*) FROM A ...

    数据库学习

    SELECT * FROM 表名 ORDER BY CASE WHEN IFNULL(字段名, '') = '' THEN 0 ELSE 1 END, 字段名 DESC; ``` 4. **合并列值**:在MySQL中,可以使用`GROUP_CONCAT(字段名 SEPARATOR 分隔符)`函数将多行数据的某个字段...

Global site tag (gtag.js) - Google Analytics