我们可以多次使用由Spring提供的sessionFactory吗?

时间:2013-05-08 06:05:12

标签: java java-ee

但是在我的代码中,当我第二次使用会话工厂时,我得到空指针异常。它首先工作正常。

这是我的代码

<bean id="dataSource"
    class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
    <property name="username" value="root"></property>
    <property name="password" value="root"></property>
    <property name="url" value="jdbc:mysql://localhost:3306/pdevice"></property>
</bean>

<bean id="sessionFactory"
    class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    <property name="dataSource" ref="dataSource"></property>
    <property name="hibernateProperties">
        <props>
            <prop key="show_sql">true</prop>
            <prop key="dialect">org.hibernate.dialect.MySQL5Dialect</prop>
        </props>
    </property>
    <property name="mappingResources">
        <array>
            <value>/LoginDTO.hbm.xml</value>
        </array>
    </property>
</bean>

<bean id="loginDAO" class="com.raj.sprmvc.dao.LoginDAOImpl">
    <property name="sessionFactory" ref="sessionFactory"></property>
</bean>

<bean id="loginManager" class="com.raj.sprmvc.manager.LoginManagerImpl">
    <property name="loginDAO" ref="loginDAO"></property>
</bean>

<!-- <bean id="login" class="com.raj.sprmvc.controller.LoginController"> 
    <property name="loginManager" ref="loginManager"></property> </bean> -->


<bean id="commandController" name="/login.do" class="com.raj.sprmvc.controller.LoginController">
    <property name="loginManager" ref="loginManager"></property>
    <property name="commandClass" value="com.raj.sprmvc.dto.LoginDTO"></property>
    <property name="validator" ref="loginValidator"></property>
</bean>

<bean id="loginValidator" class="com.raj.sprmvc.validator.LoginValidator"></bean>


<!-- ++++++++++++++++++++++to update place+++++++++++++++++++++ -->




<bean id="updateUNDAO" class="com.raj.sprmvc.dao.UpdateUNDAOImpl">
     <property name="sessionFactory" ref="sessionFactory"></property> 
</bean>

<bean id="updateManager" class="com.raj.sprmvc.manager.UpdateManagerImpl">
    <property name="undao" ref="updateUNDAO"></property>
</bean>

<bean id="commandController1" name="/update.do"
    class="com.raj.sprmvc.controller.UpdateController">
    <property name="updateManager" ref="updateManager"></property>
    <property name="commandClass" value="com.raj.sprmvc.dto.LoginDTO"></property>
    <!-- <property name="validator" ref="loginValidator"></property> -->
</bean>

当我试图第二次使用sessinfactory时,它正在给出nullpointer ..我知道这个基本概念,但努力解决这个问题分享....... 感谢

这是第一个模块 公共类LoginDAOImpl实现了LoginDAO {

private SessionFactory sessionFactory;

@Override
public boolean isExist(LoginDTO ldto) {

    Session session=sessionFactory.openSession();

    Query query=session.createQuery(" FROM LoginDTO WHERE username='"+ldto.getUsername()+"'  AND  password='"+ldto.getPassword()+"'");


    if(query.uniqueResult()!=null){
        return true;
    }
    return false;
}

public SessionFactory getSessionFactory() {
    return sessionFactory;
}

public void setSessionFactory(SessionFactory sessionFactory) {
    this.sessionFactory = sessionFactory;
}

}

和第二个模块

公共类UpdateUNDAOImpl实现UpdateUNDAO {

private SessionFactory sessionFactory;
public SessionFactory getSessionFactory() {
    return sessionFactory;
}

public void setSessionFactory(SessionFactory sessionFactory) {
    this.sessionFactory = sessionFactory;
}

public void update(LoginDTO ldto){

    Session session=sessionFactory.openSession();
    System.out.println(session);

交易tr = session.beginTransaction();         tr.begin();

    LoginDTO ldto1=(LoginDTO)session.load(LoginDTO.class, 1);
    ldto1.setPlace(ldto.getPlace());

    session.update(ldto1);


    tr.commit();
    session.close();

} }

在这个模块中,Session session = sessionFactory.openSession();给出空指针异常

例外 例外

org.springframework.web.util.NestedServletException:请求处理失败;嵌套异常是java.lang.NullPointerException     org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:659)     org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:563)     javax.servlet.http.HttpServlet.service(HttpServlet.java:641)     javax.servlet.http.HttpServlet.service(HttpServlet.java:722)

注意Apache Tomcat / 7.0.27日志中提供了根本原因的完整堆栈跟踪。

例外

0 个答案:

没有答案