query.list()上的Hibernate NullPointerException?

时间:2016-08-12 13:16:29

标签: java hibernate

嗨所有当我试图从getNamedQuery语句调用DB存储过程时,有时遇到NPE并且无法调试代码,因为Db存储过程正在正确执行并返回ref cursur。 似乎将resultSet变为NULL并且在将ResultSet转换为List()时它会生成NPE,但不确定:(?

ERROR com.uil.dat.dao.impl.WorkOrderDAOImpl: Exception occurred on calculatePriority DAO method
    java.lang.NullPointerException
            at org.hibernate.loader.custom.CustomLoader$Metadata.<init>(CustomLoader.java:552)
            at org.hibernate.loader.custom.CustomLoader.autoDiscoverTypes(CustomLoader.java:525)
            at org.hibernate.loader.Loader.getResultSet(Loader.java:1878)
            at org.hibernate.loader.Loader.doQuery(Loader.java:718)
            at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:270)
            at org.hibernate.loader.Loader.doList(Loader.java:2449)
            at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2192)
            at org.hibernate.loader.Loader.list(Loader.java:2187)
            at org.hibernate.loader.custom.CustomLoader.list(CustomLoader.java:316)
            at org.hibernate.impl.SessionImpl.listCustomQuery(SessionImpl.java:1832)
            at org.hibernate.impl.AbstractSessionImpl.list(AbstractSessionImpl.java:165)
            at org.hibernate.impl.SQLQueryImpl.list(SQLQueryImpl.java:179)
            at com.uil.dat.dao.impl.WorkOrderDAOImpl.calculatePriority(WorkOrderDAOImpl.java:60)
            at com.uil.dat.manager.impl.DAOManagerImpl.calculatePriority(DAOManagerImpl.java:154)
            at com.uil.dat.service.spt.impl.WorkOrderServiceImpl.calcPriority(WorkOrderServiceImpl.java:157)
            at com.uil.dat.controller.spt.WorkOrderControllerBean.calcPriority(WorkOrderControllerBean.java:308)
            at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
            at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:60)
            at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:37)
            at java.lang.reflect.Method.invoke(Method.java:611)
            at org.apache.el.parser.AstValue.invoke(AstValue.java:268)
            at org.apache.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:278)
            at org.apache.myfaces.view.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:83)
            at javax.faces.component._MethodExpressionToMethodBinding.invoke(_MethodExpressionToMethodBinding.java:88)
            at org.apache.myfaces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:100)
            at javax.faces.component.UICommand.broadcast(UICommand.java:120)
            at javax.faces.component.UIViewRoot._broadcastAll(UIViewRoot.java:995)
            at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:278)
            at javax.faces.component.UIViewRoot._process(UIViewRoot.java:1307)
            at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:733)
            at org.apache.myfaces.lifecycle.InvokeApplicationExecutor.execute(InvokeApplicationExecutor.java:34)
            at org.apache.myfaces.lifecycle.LifecycleImpl.executePhase(LifecycleImpl.java:172)
            at org.apache.myfaces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:119)
            at javax.faces.webapp.FacesServlet.service(FacesServlet.java:189)
            at com.ibm.ws.webcontainer.servlet.ServletWrapper.service(ServletWrapper.java:1230)
            at com.ibm.ws.webcontainer.servlet.ServletWrapper.handleRequest(ServletWrapper.java:779)
            at com.ibm.ws.webcontainer.servlet.ServletWrapper.handleRequest(ServletWrapper.java:478)
            at com.ibm.ws.webcontainer.servlet.ServletWrapperImpl.handleRequest(ServletWrapperImpl.java:178)
            at com.ibm.ws.webcontainer.filter.WebAppFilterChain.invokeTarget(WebAppFilterChain.java:136)
            at com.ibm.ws.webcontainer.filter.WebAppFilterChain.doFilter(WebAppFilterChain.java:97)
            at com.uil.dat.filter.RequestFilter.doFilter(RequestFilter.java:74)
            at com.ibm.ws.webcontainer.filter.FilterInstanceWrapper.doFilter(FilterInstanceWrapper.java:195)
            at com.ibm.ws.webcontainer.filter.WebAppFilterChain.doFilter(WebAppFilterChain.java:91)
            at com.ibm.ws.webcontainer.filter.WebAppFilterManager.doFilter(WebAppFilterManager.java:964)
            at com.ibm.ws.webcontainer.filter.WebAppFilterManager.invokeFilters(WebAppFilterManager.java:1104)
            at com.ibm.ws.webcontainer.servlet.CacheServletWrapper.handleRequest(CacheServletWrapper.java:87)
            at com.ibm.ws.webcontainer.WebContainer.handleRequest(WebContainer.java:914)
            at com.ibm.ws.webcontainer.WSWebContainer.handleRequest(WSWebContainer.java:1662)
            at com.ibm.ws.webcontainer.channel.WCChannelLink.ready(WCChannelLink.java:200)
            at com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.handleDiscrimination(HttpInboundLink.java:463)
            at com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.handleNewRequest(HttpInboundLink.java:530)
            at com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.processRequest(HttpInboundLink.java:316)
            at com.ibm.ws.http.channel.inbound.impl.HttpICLReadCallback.complete(HttpICLReadCallback.java:88)
            at com.ibm.ws.tcp.channel.impl.AioReadCompletionListener.futureCompleted(AioReadCompletionListener.java:175)
            at com.ibm.io.async.AbstractAsyncFuture.invokeCallback(AbstractAsyncFuture.java:217)
            at com.ibm.io.async.AsyncChannelFuture.fireCompletionActions(AsyncChannelFuture.java:161)
            at com.ibm.io.async.AsyncFuture.completed(AsyncFuture.java:138)
            at com.ibm.io.async.ResultHandler.complete(ResultHandler.java:204)
            at com.ibm.io.async.ResultHandler.runEventProcessingLoop(ResultHandler.java:775)
            at com.ibm.io.async.ResultHandler$2.run(ResultHandler.java:905)
            at com.ibm.ws.util.ThreadPool$Worker.run(ThreadPool.java:1865)
    [12/08/16 06:50:33:033 EDT] Thread:[WebContainer : 5]  WARN org.hibernate.jdbc.AbstractBatcher: Could not close a JDBC result set
    java.lang.NullPointerException
            at org.hibernate.jdbc.AbstractBatcher.closeStatements(AbstractBatcher.java:338)
            at org.hibernate.jdbc.ConnectionManager.cleanup(ConnectionManager.java:405)
            at org.hibernate.jdbc.ConnectionManager.close(ConnectionManager.java:347)
            at org.hibernate.impl.SessionImpl.close(SessionImpl.java:335)
            at com.uil.dat.util.HibernateUtil.close(HibernateUtil.java:54)
            at com.uil.dat.dao.impl.WorkOrderDAOImpl.calculatePriority(WorkOrderDAOImpl.java:94)
            at com.uil.dat.manager.impl.DAOManagerImpl.calculatePriority(DAOManagerImpl.java:154)
            at com.uil.dat.service.spt.impl.WorkOrderServiceImpl.calcPriority(WorkOrderServiceImpl.java:157)
            at com.uil.dat.controller.spt.WorkOrderControllerBean.calcPriority(WorkOrderControllerBean.java:308)
            at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
            at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:60)
            at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:37)
            at java.lang.reflect.Method.invoke(Method.java:611)
            at org.apache.el.parser.AstValue.invoke(AstValue.java:268)
            at org.apache.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:278)
            at org.apache.myfaces.view.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:83)
            at javax.faces.component._MethodExpressionToMethodBinding.invoke(_MethodExpressionToMethodBinding.java:88)
            at org.apache.myfaces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:100)
            at javax.faces.component.UICommand.broadcast(UICommand.java:120)
            at javax.faces.component.UIViewRoot._broadcastAll(UIViewRoot.java:995)
            at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:278)
            at javax.faces.component.UIViewRoot._process(UIViewRoot.java:1307)
            at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:733)
            at org.apache.myfaces.lifecycle.InvokeApplicationExecutor.execute(InvokeApplicationExecutor.java:34)
            at org.apache.myfaces.lifecycle.LifecycleImpl.executePhase(LifecycleImpl.java:172)
            at org.apache.myfaces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:119)
            at javax.faces.webapp.FacesServlet.service(FacesServlet.java:189)
            at com.ibm.ws.webcontainer.servlet.ServletWrapper.service(ServletWrapper.java:1230)
            at com.ibm.ws.webcontainer.servlet.ServletWrapper.handleRequest(ServletWrapper.java:779)
            at com.ibm.ws.webcontainer.servlet.ServletWrapper.handleRequest(ServletWrapper.java:478)
            at com.ibm.ws.webcontainer.servlet.ServletWrapperImpl.handleRequest(ServletWrapperImpl.java:178)
            at com.ibm.ws.webcontainer.filter.WebAppFilterChain.invokeTarget(WebAppFilterChain.java:136)
            at com.ibm.ws.webcontainer.filter.WebAppFilterChain.doFilter(WebAppFilterChain.java:97)
            at com.uil.dat.filter.RequestFilter.doFilter(RequestFilter.java:74)
            at com.ibm.ws.webcontainer.filter.FilterInstanceWrapper.doFilter(FilterInstanceWrapper.java:195)
            at com.ibm.ws.webcontainer.filter.WebAppFilterChain.doFilter(WebAppFilterChain.java:91)
            at com.ibm.ws.webcontainer.filter.WebAppFilterManager.doFilter(WebAppFilterManager.java:964)
            at com.ibm.ws.webcontainer.filter.WebAppFilterManager.invokeFilters(WebAppFilterManager.java:1104)
            at com.ibm.ws.webcontainer.servlet.CacheServletWrapper.handleRequest(CacheServletWrapper.java:87)
            at com.ibm.ws.webcontainer.WebContainer.handleRequest(WebContainer.java:914)
            at com.ibm.ws.webcontainer.WSWebContainer.handleRequest(WSWebContainer.java:1662)
            at com.ibm.ws.webcontainer.channel.WCChannelLink.ready(WCChannelLink.java:200)
            at com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.handleDiscrimination(HttpInboundLink.java:463)
            at com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.handleNewRequest(HttpInboundLink.java:530)
            at com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.processRequest(HttpInboundLink.java:316)
            at com.ibm.ws.http.channel.inbound.impl.HttpICLReadCallback.complete(HttpICLReadCallback.java:88)
            at com.ibm.ws.tcp.channel.impl.AioReadCompletionListener.futureCompleted(AioReadCompletionListener.java:175)
            at com.ibm.io.async.AbstractAsyncFuture.invokeCallback(AbstractAsyncFuture.java:217)
            at com.ibm.io.async.AsyncChannelFuture.fireCompletionActions(AsyncChannelFuture.java:161)
            at com.ibm.io.async.AsyncFuture.completed(AsyncFuture.java:138)
            at com.ibm.io.async.ResultHandler.complete(ResultHandler.java:204)
            at com.ibm.io.async.ResultHandler.runEventProcessingLoop(ResultHandler.java:775)
            at com.ibm.io.async.ResultHandler$2.run(ResultHandler.java:905)
            at com.ibm.ws.util.ThreadPool$Worker.run(ThreadPool.java:1865)
    [12/08/16 06:50:33:033 EDT] Thread:[WebContainer : 5]  INFO com.uil.dat.

目前我正在使用Hibernate3.jar和JSF。

我的调用DB存储过程的代码如下:

try {
            session = HibernateUtil.buildIfNeeded().openSession();
            if(null!=session && session.isOpen()){
                HibernateUtil.beginTransaction(session);
            }
            logger.info(Constant.INITIATING_DB_CONNECTION);//building session from sessionFactory if not created
            Query query = session.getNamedQuery("callCalculatePriorityProcedure");
            query.setResultTransformer(Transformers.aliasToBean(ProcedureReturn.class));
            if(null!= query){
                List<ProcedureReturn> calculatePriority = query.list();
                if(null!= calculatePriority && calculatePriority.size()>0){
                    for(ProcedureReturn procedureReturn:calculatePriority){
                        if(Integer.parseInt(procedureReturn.getRETURNINTEGER())==0){
                            result = 0;
                            logger.info("procedure has bene executed successfully");
                        }
                        else{
                            result = 1;
                            logger.info("procedure wasn't executed successfully");
                        }
                    }
                }else{
                    result = 2;
                }   
            }

1 个答案:

答案 0 :(得分:0)

现在,当我找到问题的根本原因时,它无法从java hibernate方面解决。我们正在使用数据库链接从视图中获取数据,因为一些故障DB链接有时无法获取任何记录并继续进入异常部分并且不返回任何内容。现在我们需要在DB过程本身处理数据库异常,以便即使发生异常也会返回一些值,这样就可以确定DB链接由于某种原因而失效。 顺便说一句,谢谢@Thomas。