Hibernate获取SQL query.list():ArrayIndexOutOfBoundsException 0

时间:2013-05-09 12:42:14

标签: java sql hibernate indexoutofboundsexception

@SuppressWarnings("unchecked")
public List<Object[]> findAdcampStatistics(Long adcampId, String startDate, String endDate, float costPerClick, String orderStatus){
    String sqlStr = 
            "SELECT adcamp_id, alternative_ids, click, orderCount, conversion, ((100*orderCount)/click) AS conversionRate, (click * " + (costPerClick != 0?costPerClick:0) + ") AS cost " +
            "FROM ( " +
                "SELECT adcamp_id, alternative_ids, COUNT(*) AS click, " +
                    "SUM(( " + 
                        "SELECT COUNT(*) "+
                        "FROM nc_order, nc_cart "+
                        "WHERE nc_order.cart_id = nc_cart.id "+
                            "AND nc_cart.adcamp_click_id = nc_adcamp_click.id "+
                            (orderStatus != ""? "AND nc_order.order_status = '"+ orderStatus +"' " : " ")+
                        " )) AS orderCount, "+
                    "SUM(( " + 
                        "SELECT SUM(nc_payment_log.amount) "+
                        "FROM nc_order, nc_cart, nc_payment_log "+
                        "WHERE nc_order.payment_log_id = nc_payment_log.id "+
                            "AND nc_order.cart_id = nc_cart.id "+
                            "AND nc_cart.adcamp_click_id = nc_adcamp_click.id " +  
                            (orderStatus != ""? "AND nc_order.order_status = '"+ orderStatus +"' " : " ") +
                        " )) AS conversion "+
                "FROM nc_adcamp_click "+
                "WHERE "+ 
                    (adcampId!=0? "adcamp_id =" + adcampId.toString() + " AND ":" ") +
                    "date_clicked BETWEEN '" + startDate + "' AND '" + endDate + "' "+
                "GROUP BY alternative_ids "+
            ") AS temp";

    SQLQuery sqlQuery = getSQLQuery(sqlStr);
    System.out.println("ADCAMP SQL ===============> " + sqlStr);
    List<Object[]> result = new ArrayList<Object[]>();

    return sqlQuery.list();
}

这是一个有点复杂的sql,我是使用params创建它的,当我在行sqlQuery.list();上运行它时会出现这个错误:

java.lang.ArrayIndexOutOfBoundsException: 0
at org.hibernate.type.TypeHelper.disassemble(TypeHelper.java:146)
at org.hibernate.cache.StandardQueryCache.put(StandardQueryCache.java:106)
at org.hibernate.loader.Loader.putResultInQueryCache(Loader.java:2443)
at org.hibernate.loader.Loader.listUsingQueryCache(Loader.java:2321)
at org.hibernate.loader.Loader.list(Loader.java:2268)
at org.hibernate.loader.custom.CustomLoader.list(CustomLoader.java:316)
at org.hibernate.impl.SessionImpl.listCustomQuery(SessionImpl.java:1842)
at org.hibernate.impl.AbstractSessionImpl.list(AbstractSessionImpl.java:165)
at org.hibernate.impl.SQLQueryImpl.list(SQLQueryImpl.java:157)
at com.netasoft.commerce.framework.marketing.dao.AdCampDaoImpl.findAdcampStatistics(AdCampDaoImpl.java:71)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:318)
at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:183)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:110)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202)
at $Proxy96.findAdcampStatistics(Unknown Source)
at com.netasoft.commerce.framework.marketing.service.MarketingServiceImpl.findAdcampStatistics(MarketingServiceImpl.java:71)
at com.netasoft.commerce.framework.adminint.controller.ReportController.adcamp(ReportController.java:55)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:212)
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:126)
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:96)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:617)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:578)
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:80)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:900)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:827)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:882)
at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:789)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:710)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)

然而,当我在查询浏览器中的sql查询上运行脚本时,它无例外地工作并返回结果。

SQL是:

SELECT adcamp_id, alternative_ids, click, orderCount, conversion, (100*orderCount)/click AS conversionRate, (click * 1.0) AS cost 
FROM ( SELECT adcamp_id, alternative_ids, COUNT(*) AS click, 
   SUM(( SELECT COUNT(*) FROM nc_order, nc_cart WHERE nc_order.cart_id = nc_cart.id AND nc_cart.adcamp_click_id = nc_adcamp_click.id AND nc_order.order_status = 'delivered'  )) AS orderCount, 
   SUM(( SELECT SUM(nc_payment_log.amount) FROM nc_order, nc_cart, nc_payment_log WHERE nc_order.payment_log_id = nc_payment_log.id AND nc_order.cart_id = nc_cart.id AND nc_cart.adcamp_click_id = nc_adcamp_click.id AND nc_order.order_status = 'delivered'  )) AS conversion 
   FROM nc_adcamp_click 
   WHERE adcamp_id =1 AND date_clicked BETWEEN '2013-02-09' AND '2013-05-09' 
   GROUP BY alternative_ids 
) AS temp

1 个答案:

答案 0 :(得分:2)

我认为你的问题可能与此有关:

https://hibernate.atlassian.net/browse/HHH-6681

查看此本机查询的堆栈跟踪中的StandardQueryCache很奇怪。您是否明确为此查询启用了查询缓存?如果是这样,请尝试使用无查询缓存,因为看起来hibernate在导出映射实体的类型以将缓存绑定到的问题上存在问题。