case when 无法执行,为什么 HibernateJavaORMDAOServlet
程序员文章站
2022-07-10 17:56:42
...
Hql 语句:
select sum(case when to_char(t.accountinoutDate,'yyyymmdd hh24:mi:ss') < to_char(to_date('2010-07-01 00:00:00','yyyy-mm-dd hh24:mi:ss'),'yyyymmdd hh24:mi:ss') then (case when t.accountinoutDirection = 0 then 1 else -1 end) * t.accountinoutMoney end) as startmoney, t.inneraccountsCode,t.orgCode from FundAccountinout t
group by t.inneraccountsCode,t.orgCode
报错:
17:43:09,312 ERROR [[default]] Servlet.service() for servlet default threw exception
org.hibernate.hql.ast.QuerySyntaxException: fundAccountinout is not mapped [select (case when t.accountinoutDirection = 0 then 1 else -1 end) * t.accountinoutMoney as startmoney, t.inneraccountsCode,t.orgCode from fundAccountinout t where to_char(t.accountinoutDate,'yyyymmdd hh24:mi:ss') <= to_char(to_date('2010-07-30 00:00:00','yyyy-mm-dd hh24:mi:ss'),'yyyymmdd hh24:mi:ss') and t.memberCode = ? group by t.inneraccountsCode,t.orgCode]
at org.hibernate.hql.ast.util.SessionFactoryHelper.requireClassPersister(SessionFactoryHelper.java:181)
at org.hibernate.hql.ast.tree.FromElementFactory.addFromElement(FromElementFactory.java:110)
at org.hibernate.hql.ast.tree.FromClause.addFromElement(FromClause.java:93)
at org.hibernate.hql.ast.HqlSqlWalker.createFromElement(HqlSqlWalker.java:277)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.fromElement(HqlSqlBaseWalker.java:3056)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.fromElementList(HqlSqlBaseWalker.java:2945)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.fromClause(HqlSqlBaseWalker.java:688)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.query(HqlSqlBaseWalker.java:544)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.selectStatement(HqlSqlBaseWalker.java:281)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.statement(HqlSqlBaseWalker.java:229)
at org.hibernate.hql.ast.QueryTranslatorImpl.analyze(QueryTranslatorImpl.java:251)
at org.hibernate.hql.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:183)
at org.hibernate.hql.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:134)
at org.hibernate.engine.query.HQLQueryPlan.<init>(HQLQueryPlan.java:101)
at org.hibernate.engine.query.HQLQueryPlan.<init>(HQLQueryPlan.java:80)
at org.hibernate.engine.query.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:94)
at org.hibernate.impl.AbstractSessionImpl.getHQLQueryPlan(AbstractSessionImpl.java:156)
at org.hibernate.impl.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:135)
at org.hibernate.impl.SessionImpl.createQuery(SessionImpl.java:1650)
at org.springframework.orm.hibernate3.HibernateTemplate$30.doInHibernate(HibernateTemplate.java:923)
at org.springframework.orm.hibernate3.HibernateTemplate.doExecute(HibernateTemplate.java:419)
at org.springframework.orm.hibernate3.HibernateTemplate.executeWithNativeSession(HibernateTemplate.java:374)
at org.springframework.orm.hibernate3.HibernateTemplate.find(HibernateTemplate.java:921)
at com.golden.soft.common.system.dao.HibernateGenericDao.getResultCount(HibernateGenericDao.java:635)
at com.golden.soft.common.system.dao.HibernateGenericDao.queryForPageByHql(HibernateGenericDao.java:330)
原因是红色部分,即类名写错了,严格区分大小写
case when在hibernate中是可以使用的,但是语法比较严格
hql语句应改为(注意红色部分)
select sum(case when to_char(t.accountinoutDate,'yyyymmdd hh24:mi:ss') < to_char(to_date('2010-07-01 00:00:00','yyyy-mm-dd hh24:mi:ss'),'yyyymmdd hh24:mi:ss') then ((case when t.accountinoutDirection = 0 then 1 else -1 end) * t.accountinoutMoney) end) as startmoney, t.inneraccountsCode,t.orgCode from FundAccountinout t
group by t.inneraccountsCode,t.orgCode
另外注意,xml中需要<prop key="hibernate.query.factory_class">org.hibernate.hql.ast.ASTQueryTranslatorFactory</prop>
select sum(case when to_char(t.accountinoutDate,'yyyymmdd hh24:mi:ss') < to_char(to_date('2010-07-01 00:00:00','yyyy-mm-dd hh24:mi:ss'),'yyyymmdd hh24:mi:ss') then (case when t.accountinoutDirection = 0 then 1 else -1 end) * t.accountinoutMoney end) as startmoney, t.inneraccountsCode,t.orgCode from FundAccountinout t
group by t.inneraccountsCode,t.orgCode
报错:
17:43:09,312 ERROR [[default]] Servlet.service() for servlet default threw exception
org.hibernate.hql.ast.QuerySyntaxException: fundAccountinout is not mapped [select (case when t.accountinoutDirection = 0 then 1 else -1 end) * t.accountinoutMoney as startmoney, t.inneraccountsCode,t.orgCode from fundAccountinout t where to_char(t.accountinoutDate,'yyyymmdd hh24:mi:ss') <= to_char(to_date('2010-07-30 00:00:00','yyyy-mm-dd hh24:mi:ss'),'yyyymmdd hh24:mi:ss') and t.memberCode = ? group by t.inneraccountsCode,t.orgCode]
at org.hibernate.hql.ast.util.SessionFactoryHelper.requireClassPersister(SessionFactoryHelper.java:181)
at org.hibernate.hql.ast.tree.FromElementFactory.addFromElement(FromElementFactory.java:110)
at org.hibernate.hql.ast.tree.FromClause.addFromElement(FromClause.java:93)
at org.hibernate.hql.ast.HqlSqlWalker.createFromElement(HqlSqlWalker.java:277)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.fromElement(HqlSqlBaseWalker.java:3056)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.fromElementList(HqlSqlBaseWalker.java:2945)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.fromClause(HqlSqlBaseWalker.java:688)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.query(HqlSqlBaseWalker.java:544)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.selectStatement(HqlSqlBaseWalker.java:281)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.statement(HqlSqlBaseWalker.java:229)
at org.hibernate.hql.ast.QueryTranslatorImpl.analyze(QueryTranslatorImpl.java:251)
at org.hibernate.hql.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:183)
at org.hibernate.hql.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:134)
at org.hibernate.engine.query.HQLQueryPlan.<init>(HQLQueryPlan.java:101)
at org.hibernate.engine.query.HQLQueryPlan.<init>(HQLQueryPlan.java:80)
at org.hibernate.engine.query.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:94)
at org.hibernate.impl.AbstractSessionImpl.getHQLQueryPlan(AbstractSessionImpl.java:156)
at org.hibernate.impl.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:135)
at org.hibernate.impl.SessionImpl.createQuery(SessionImpl.java:1650)
at org.springframework.orm.hibernate3.HibernateTemplate$30.doInHibernate(HibernateTemplate.java:923)
at org.springframework.orm.hibernate3.HibernateTemplate.doExecute(HibernateTemplate.java:419)
at org.springframework.orm.hibernate3.HibernateTemplate.executeWithNativeSession(HibernateTemplate.java:374)
at org.springframework.orm.hibernate3.HibernateTemplate.find(HibernateTemplate.java:921)
at com.golden.soft.common.system.dao.HibernateGenericDao.getResultCount(HibernateGenericDao.java:635)
at com.golden.soft.common.system.dao.HibernateGenericDao.queryForPageByHql(HibernateGenericDao.java:330)
原因是红色部分,即类名写错了,严格区分大小写
case when在hibernate中是可以使用的,但是语法比较严格
hql语句应改为(注意红色部分)
select sum(case when to_char(t.accountinoutDate,'yyyymmdd hh24:mi:ss') < to_char(to_date('2010-07-01 00:00:00','yyyy-mm-dd hh24:mi:ss'),'yyyymmdd hh24:mi:ss') then ((case when t.accountinoutDirection = 0 then 1 else -1 end) * t.accountinoutMoney) end) as startmoney, t.inneraccountsCode,t.orgCode from FundAccountinout t
group by t.inneraccountsCode,t.orgCode
另外注意,xml中需要<prop key="hibernate.query.factory_class">org.hibernate.hql.ast.ASTQueryTranslatorFactory</prop>