未找到列'orderId'。 com.liferay.portal.kernel.dao.orm.ORMException:org.hibernate.exception.SQLGrammarException:无法执行查询

时间:2013-09-14 05:00:39

标签: mysql liferay

以下是自定义SQL:

SELECT COUNT(*) 
FROM EEC_Order 
WHERE DATE(EEC_Order.createDate) = CURDATE() 
    AND EEC_Order.status = ? 
    AND EEC_Order.companyId = ?

以上sql查询在mysql中工作但不能正常工作自定义sql登录liferay

public class OrderFinderImpl extends BasePersistenceImpl<Order> implements OrderFinder    {
    public int getTodayOrderCount(String status,long companyId) {
        Session session = null;
        SQLQuery query = null;
        try {

            String sql = CustomSQLUtil.get(GET_TODAY_ORDER_COUNT);          
            session = openSession();            
            query = session.createSQLQuery(sql);            
            query.addEntity("Order", OrderImpl.class);          
            QueryPos qPos = QueryPos.getInstance(query);            
            qPos.add(status);
            qPos.add(companyId);            
            Iterator<Long> itr = query.list().iterator();
                if (itr.hasNext()) {
                Long count = itr.next();
                    if (count != null) {                
                     return count.intValue();               
                    }
                }                       
            }               
            catch (Exception e) {               
                e.printStackTrace();
            }               
            finally {               
            closeSession(session);              
            }
        return 0;           
    }
    public static String GET_TODAY_ORDER_COUNT = "getTodayOrderCount";

}

控制台例外:

04:56:56,520 ERROR [http-bio-8080-exec-6][JDBCExceptionReporter:76] Column 'orderId' not found.
com.liferay.portal.kernel.dao.orm.ORMException: org.hibernate.exception.SQLGrammarException: could not execute query
    at com.liferay.portal.dao.orm.hibernate.ExceptionTranslator.translate(ExceptionTranslator.java:30)
    at com.liferay.portal.dao.orm.hibernate.SQLQueryImpl.list(SQLQueryImpl.java:111)
    at com.liferay.portal.dao.orm.hibernate.SQLQueryImpl.list(SQLQueryImpl.java:88)
    at com.esquare.ecommerce.service.persistence.OrderFinderImpl.getTodayOrderCount(OrderFinderImpl.java:37)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:320)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:183)    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150)
    at com.liferay.portal.dao.shard.advice.ShardPersistenceAdvice.invoke(ShardPersistenceAdvice.java:52)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
    at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:90)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
    at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202)
    at com.sun.proxy.$Proxy426.getTodayOrderCount(Unknown Source)

1 个答案:

答案 0 :(得分:4)

这是因为您的代码中包含以下行:

query.addEntity("Order", OrderImpl.class);

当sql查询返回构造Order实体的所有列时使用,例如使用上述语句查询应该是:

SELECT * FROM EEC_ORDER

由于您的查询只返回一个聚合函数COUNT,它会返回标量值,单个值,因此您不能将addEntity方法与计数查询一起使用。

addEntity方法将获得结果集并尝试通过使用类似Order之类的内容逐个设置所有列来在order.setOrderId(resultSet.getLong("orderId"))实体中设置它,因为您的sql查询未返回任何orderId或其他列失败,您将获得上述异常。

那么现在该怎么办?继续阅读解决方案:

要在查询中返回long类型的标量,您可以使用以下方法代替addEntity方法:

query.addScalar("countOfOrder", Type.LONG);

其中countOfOrder只是sql查询中count的别名,例如

SELECT count(*) as "countOfOrder" FROM EEC_ORDER