写入时的hibernate空指针异常

时间:2017-10-04 06:04:33

标签: java spring hibernate nullpointerexception hql

我正在开发一个包含hibernate的spring项目。 我经常遇到从数据库中获取对象的问题 错误如下:

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实现中出现:

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

我正在为我的配置使用注释而不是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>

我会上传我的代码的任何部分,以帮助我处理我的问题,谢谢

更新:

我似乎已经通过改变解决了     c.getName() 至     c.getSimpleName() 为了获得班级名称

1 个答案:

答案 0 :(得分:0)

奇怪!!!

请您修改一下这样的查询并试一试吗?我添加了&#39; AS&#39;在别名之前。如果事实证明这是问题,我会感到惊讶。

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