2

以下是自定义 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)
4

1 回答 1

5

发生这种情况是因为您的代码中有以下行:

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);

wherecountOfOrder只不过count是 sql 查询中的别名,例如

SELECT count(*) as "countOfOrder" FROM EEC_ORDER
于 2013-09-14T08:37:13.057 回答