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的测试用例里明明有这样的程序(一部分代码):
[code]
// $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();
}
}
[/code]
解决办法:
终于发现问题了,配置文件问题.
当查询解析器配置为
代码
<property name="hibernate.query.factory_class">org.hibernate.hql.classic.ClassicQueryTranslatorFactory</property>
出错
配置为
代码
<property name="org.hibernate.hql.ast.ASTQueryTranslatorFactory"</property>
能正常运行.
怀疑第一个解析器有问题.
感谢:together、BirdGu的帮助!
执行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的测试用例里明明有这样的程序(一部分代码):
[code]
// $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();
}
}
[/code]
解决办法:
终于发现问题了,配置文件问题.
当查询解析器配置为
代码
<property name="hibernate.query.factory_class">org.hibernate.hql.classic.ClassicQueryTranslatorFactory</property>
出错
配置为
代码
<property name="org.hibernate.hql.ast.ASTQueryTranslatorFactory"</property>
能正常运行.
怀疑第一个解析器有问题.
感谢:together、BirdGu的帮助!