Spring MVC With Hibernate;嵌套异常是java.lang.NullPointerException当getCurrentSession()

时间:2016-01-14 09:13:54

标签: java spring hibernate maven spring-mvc

我收到此错误严重:Servlet [appServlet]的Servlet.service()在路径[/ org]的上下文中引发异常[请求处理失败;嵌套异常是带有根本原因的java.lang.NullPointerException]

java.lang.NullPointerException
    at com.aquicktrack.controller.RegistrationDao.insertCustomer(RegistrationDao.java:31)
    at com.aquicktrack.org.StratUpController.getRegistration(StratUpController.java:32)
    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.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:215)
    at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:132)
    at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:749)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:689)
    at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:83)
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:938)
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:870)
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:961)
    at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:852)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:622)
    at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:837)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:212)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:141)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
    at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:616)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:521)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1096)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:674)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1500)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1456)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Unknown Source)

我的Spring配置文件

<!-- JDBC Data Source. It is assumed you have MySQL running on localhost 
        port 3306 with username root and blank password. Change below if it's not 
        the case -->
    <bean id="myDataSource" class="org.apache.tomcat.dbcp.dbcp2.BasicDataSource"
        destroy-method="close">
        <property name="driverClassName" value="com.mysql.jdbc.Driver" />
        <property name="url" value="jdbc:mysql://localhost:3306/study" />
        <property name="username" value="root" />
        <property name="password" value="" />
        <property name="validationQuery" value="SELECT 1" />
    </bean>

    <bean id="sessionFactory"
        class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="dataSource" ref="myDataSource" />
        <property name="packagesToScan" value="com.aquicktrack.com.entities" />
        <property name="hibernateProperties">
            <props>

                <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
                <prop key="hibernate.show_sql">true</prop>
                <prop key="hibernate.hbm2ddl.auto">create</prop>
            </props>
        </property>
    </bean>

    <!-- Hibernate Transaction Manager -->
    <bean id="transactionManager"
        class="org.springframework.orm.hibernate4.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory" />
    </bean>

    <!-- Activates annotation based transaction management -->
    <tx:annotation-driven transaction-manager="transactionManager" />

和我的Java代码

@Autowired
    private SessionFactory sessionFactory;
    /**
     * Simply selects the home view to render by returning its name.
     */
    @Transactional
    public void insertCustomer()
    {
        Registration registration = new Registration();
        registration.setFullname("Ashish Srivastava");
        registration.setEmail("Ashish");
        registration.setIsRegister("0");
        registration.setPhone("xxxxxxxxxx");
        registration.setStatus("0");
        registration.setTime("1234567890");
        registration.setVehicle("12");
        Session session = this.sessionFactory.getCurrentSession();
        session.persist(registration);
        logger.info("Person saved successfully, Registration Details="+registration);
    }

当我想从会话工厂类中获取当前会话时,它会抛出它

1 个答案:

答案 0 :(得分:1)

根据评论:

  

让我猜你的控制器你做了新的RegistrationDao()   使用spring托管实例。 - M. Deinum

  

是的,我正在创建此类的实例并使用调用该方法   对象 - Ashish Srivastava

问题是Spring只会注入Spring Managed Beans *。当您使用new创建对象时,它不是Spring Managed Bean!

因此,向您的DAO添加@Repository注释,并通过添加带@Autowird注释的字段将DAO注入您的控制器,然后使用它代替new DAO

@Repository
public class YourDAO{
    @Autowired
    private SessionFactory sessionFactory;
    ...

    @Transactional
    public void insertCustomer(){...}
}

@Controller
public class YourController{
    @Autowired
    private YourDao yourDao;

    ... {
       this.yourDao.insertCustomer();
    }
}

* Except when @Configurable is used