5
回答
Spring4 Hibernate3 整合SessionFactory抛出空异常
注册华为云得mate10,2.9折抢先购!>>>   
今天在做Spring 和 Hibernate的整合时出现的问题,一直没能解决,求助:
说明:我创建的是一个web工程,然后在测试的时候使用的是Junit
看源码:

测试类:

public class Test {
    //创建Spring 工厂
    private BeanFactory factory = new ClassPathXmlApplicationContext("beans.xml");

    @org.junit.Test
    public void add(){
        IUserService userService = factory.getBean("userService", UserService.class);
        User user = new User();
        user.setName("Defens");
        userService.add(user);
    }
}
业务处理类:

public class UserService implements IUserService{

    private IUserDao userDao;

    public IUserDao getUserDao() {
        return userDao;
    }

    public void setUserDao(IUserDao userDao) {
        this.userDao = userDao;
    }

    @Override
    public void add(User user) {

        System.out.println("添加用户服务层............");

        IUserDao userDao = new UserDaoImpl();
        userDao.save(user);
    }
}
持久化操作类:

public class UserDaoImpl extends BaseDao implements IUserDao {
    
    @Override
    public void save(User user) {

        System.out.println("添加用户持久化层............");
        //从父对象获取
        Session session = getSession();
        session.save(user);
    }
}
持久化父类

public class BaseDao {

    private SessionFactory factory;

    public SessionFactory getFactory() {
        return factory;
    }

    public void setFactory(SessionFactory factory) {
        this.factory = factory;
    }

    protected Session getSession(){
        return  this.factory.openSession();
    }
}
beans.xml的配置文件

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
              <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
              <property name="url" value="jdbc:mysql://localhost:3306/demo"/>
              <property name="username" value="root"/>
              <property name="password" value="123456"/>
       </bean>

       <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
              <property name="dataSource" ref="dataSource"/>
              <!--
                     此处指明相关的hibernate资源文件
              -->
              <property name="mappingResources">
                     <list>
                            <value>User.hbm.xml</value>
                     </list>
              </property>

              <!--配置Hibernate相关的属性-->
              <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>
                            <prop key="hibernate.format_sql">false</prop>
                     </props>
              </property>
       </bean>


       <bean id="baseDao" class="cn.yesol.defens.dao.impl.BaseDao" scope="singleton">
              <property name="factory" ref="sessionFactory"/>
       </bean>

       <bean id="userDao" class="cn.yesol.defens.dao.impl.UserDaoImpl" scope="singleton">
              <!--<property name="sessionFactory" ref="sessionFactory" />-->
       </bean>

       <bean id="userService" class="cn.yesol.defens.service.impl.UserService" scope="prototype">
              <property name="userDao" ref="userDao" />
       </bean>



我整个是没有使用Annotation的,全部是基于XML的配置,在测试类运行后,查看数据库,相关的表是生成了的
错误提示如下:
添加用户服务层............
添加用户持久化层............

java.lang.NullPointerException
at cn.yesol.defens.dao.impl.BaseDao.getSession(BaseDao.java:24)
at cn.yesol.defens.dao.impl.UserDaoImpl.save(UserDaoImpl.java:19)
at cn.yesol.defens.service.impl.UserService.add(UserService.java:29)
at cn.yesol.defens.Test.Test.add(Test.java:23)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:76)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
at org.junit.runner.JUnitCore.run(JUnitCore.java:157)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:78)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:212)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:68)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140)

错误就是发生在获取SessionFactory哪里,求教,在线等,谢谢!!!

举报
Defens
发帖于3年前 5回/977阅
共有5个答案 最后回答: 3年前
刚才做了一个测试,我在测试类中直接获取了SessionFactory并打印出来了,结果是正确的,然后觉得很奇怪,回头去看配置文件的时候,突然想到Hibernate关于继承的相关内容,我这里也有一个继承关系,我就想是不是直接配置子类的bean,并把父类中的属性配置到子类中,刚才试了一下,果然是这个问题,终于得到了解决,坑了~~~~

不是有没有关闭的问题吧,感觉,这里只是获取一次就结束了整个,我做了测试,在注入的时候是注入成功了的,但是在获取的时候就是空值:

private SessionFactory factory;

    public SessionFactory getFactory() {
        System.out.println("获取SessionFactory.............");
        if(this.factory == null){
            System.out.println("SessionFactory为空.............");
        }else {
            System.out.println("SessionFactory不为空:" + this.factory.getClass().getName());
        }
        return factory;
    }

    public void setFactory(SessionFactory factory) {
        System.out.println("注入SessionFactory.............");
        if(factory == null){
            System.out.println("注入的SessionFactory为空.............");
        }else{
            System.out.println("注入的SessionFactory不为空.............");
            System.out.println("注入的SessionFactory:" + factory.getClass().getName());
        }
        this.factory = factory;
    }
输出结果:

注入SessionFactory.............
注入的SessionFactory不为空.............
注入的SessionFactory:org.hibernate.impl.SessionFactoryImpl
添加用户服务层............
添加用户持久化层............
获取SessionFactory.............
SessionFactory为空.............



这个我觉得注入应该是成功了的,我在setFactory方法里面调用getFactory是可以获取到的,但是在测试类中就不行了,我看有人说是web项目,我就把项目发到tomcat里面去,运行起来,但还是不成功。

public void setFactory(SessionFactory factory) {
        System.out.println("注入SessionFactory.............");
        if(factory == null){
            System.out.println("注入的SessionFactory为空.............");
        }else{
            System.out.println("注入的SessionFactory不为空.............");
            System.out.println("注入的SessionFactory:" + factory.getClass().getName());
        }
        this.factory = factory;

        System.out.println("######################################");
        getFactory().openSession();
        System.out.println("######################################");
    }
输出结果:

注入SessionFactory.............
注入的SessionFactory不为空.............
注入的SessionFactory:org.hibernate.impl.SessionFactoryImpl
######################################
获取SessionFactory.............
SessionFactory不为空:org.hibernate.impl.SessionFactoryImpl
######################################

顶部