2017-10-04 105 views
0

我正在研究一個包含休眠的spring項目。 我經常有問題,從數據庫 錯誤中獲取對象是如下:休眠空指針異常在作家

<code> 
    @Override 
    public Object getObject(Class c,int id)throws DsvrtException { 
     Transaction transaction = null; 
     try{ 
      transaction = getOpenSession().beginTransaction(); 
      String sql = "FROM "+c.getName()+" C WHERE C.id = :id"; 
      Query q = getOpenSession().createQuery(sql); 
      q.setParameter("id", id); 
      List list = q.list(); 
      if(list.isEmpty()) 
       throw new NoResultException("No Result"); 
      transaction.commit(); 
      return list.get(0); 
     } 
     catch(HibernateException | NoResultException|NullPointerException e){ 
      if(transaction!=null)transaction.rollback(); 
      throw new DsvrtException(c,"Failed to get Object with ID : "+id+Arrays.toString(e.getStackTrace())); 
     } 
    } 
</code> 

我使用註釋爲我的配置:

03-10-17 20:51:46 INFO DsvrtLog - IncidenceController::New Incidence Reported rape 
03-10-17 20:51:46 ERROR DsvrtLog - 
Report::Failed to get Object with ID : 1 
[java.io.Writer.write(Writer.java:157) 
java.io.BufferedWriter.newLine(BufferedWriter.java:243) 
java.io.PrintStream.newLine(PrintStream.java:544) 
java.io.PrintStream.println(PrintStream.java:807) 
org.hibernate.hql.internal.ast.util.ASTPrinter.showAsString(ASTPrinter.java:100) 
org.hibernate.hql.internal.ast.QueryTranslatorImpl.showHqlAst(QueryTranslatorImpl.java:283) 
org.hibernate.hql.internal.ast.QueryTranslatorImpl.parse(QueryTranslatorImpl.java:274) 
org.hibernate.hql.internal.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:180) 
org.hibernate.hql.internal.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:136) 
org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:105) 
org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:80) 
org.hibernate.engine.query.spi.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:168) 
org.hibernate.internal.AbstractSessionImpl.getHQLQueryPlan(AbstractSessionImpl.java:221) 
org.hibernate.internal.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:199) 
org.hibernate.internal.SessionImpl.createQuery(SessionImpl.java:1735) 
com.tivasgroups.apps.dsims.dao.DsimsDaoImpl.getObject(DsimsDaoImpl.java:108) 
com.tivasgroups.apps.dsims.service.ManageIncidenceImpl.saveIncidence(ManageIncidenceImpl.java:82) 
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) 
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
java.lang.reflect.Method.invoke(Method.java:606) 
org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:317) 
org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:190) 
org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157) 
org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:98) 
org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:262) 
org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:95) 
org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) 
org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:207) 
com.sun.proxy.$Proxy134.saveIncidence(Unknown Source) 
com.tivasgroups.apps.dsims.controller.IncidenceController.ManageIncidence(IncidenceController.java:318) 
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) 
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
java.lang.reflect.Method.invoke(Method.java:606) 
org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:214) 
org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:132) 
org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104) 
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:749) 
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:690) 
org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:83) 
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:945) 
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:876) 
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:961) 
org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:863) 
javax.servlet.http.HttpServlet.service(HttpServlet.java:648) 
org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:837) 
javax.servlet.http.HttpServlet.service(HttpServlet.java:729) 
org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:292) 
org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207) 
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) 
org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:240) 
org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207) 
org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:212) 
org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106) 
org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502) 
org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:141) 
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79) 
org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:616) 
org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88) 
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:509) 
org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1104) 
org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:684) 
org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1520) 
org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1476) 
java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145) 
java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615) 
org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) 
java.lang.Thread.run(Thread.java:745)] 

時的誤差,這一點在我的DAO實現大作而不是XML 我有一個抽象的DAO接口

<code> 
public abstract class AbstractDaoConnector { 
    public Class persistentClass; 
    private org.hibernate.id.PersistentIdentifierGenerator PK; 
    private static final ThreadLocal<Session> localThread = new ThreadLocal(); 
    @Autowired 
    protected SessionFactory sessionFactory; 
    protected Session getOpenSession()throws DsvrtException{ 
    Session session = localThread.get(); 
    if (session == null||!session.isOpen()) { 
     if(sessionFactory==null)throw new DsvrtException(AbstractDaoConnector.class,"Session Factory is null"); 
     session = sessionFactory.openSession(); 
     localThread.set(session); 
    } 
    return session; 
    } 
    public void closeSession() throws DsvrtException { 
     try{ 
     Session session = (Session) localThread.get(); 
     localThread.set(null); 
     if (session != null) { 
      session.close(); 
     } 
     } 
     catch(HibernateException e){ 
      throw new DsvrtException(AbstractDaoConnector.class,"Failed to close Session"); 
     } 
    } 
    public void persist(Object object) throws DsvrtException{ 
     getOpenSession().persist(object); 
    } 
    public void delete(Entity entity) throws DsvrtException{ 
     getOpenSession().delete(entity); 
    } 
} 
</code> 

我會上傳的任何部分我代碼將使用與我的問題幫助我,謝謝

UPDATE:

我似乎已經通過,以獲取類名稱改變 c.getName()c.getSimpleName() 解決

+0

你正在吞嚥getObject中異常的原因。我會想象你拋出的NoResultException可能是一個NPE的罪魁禍首,但你需要添加一個e.printStackTrace或者顯示這個異常消息的缺失部分以確定知道 – shinjw

+0

我已經追蹤到錯誤的起點在這裏, 'Query q = getOpenSession()。createQuery(sql);'謝謝 – Gafar

回答

0

奇怪!

你可以修改你的查詢,並給它一個鏡頭嗎?我在別名前添加了「AS」。如果事實證明這是問題,我會感到驚訝。

String sql = "FROM "+c.getName()+" AS C WHERE C.id = :id"; 
+0

這不是問題,我已經這樣做了。 npe在createQuery語句中。我想認爲它與hibernate本身有關,但在網絡上的其他地方一直沒有遇到過類似的問題。 – Gafar

+0

只是一種預感,它可能是驅動程序問題嗎? –

+0

但同樣的mysql驅動程序已經爲我所有的其他項目工作,甚至目前的,從未有過這個問題。我會檢查更新,但如果這是問題,謝謝 – Gafar