在dao数据访问层操作数据库时用同一个Session,以前JDBC用构造器获取的public class BookDao extends HibernateDaoSupport {
private Session se;

public BookDao(){
se=this.getSession();
} public void save(Book book) {
se.save(book);
}
整合SSH后报这类错误

org.springframework.beans.factory.BeanCreationException: 
Error creating bean with name 'BookDao' defined in class path resource [applicationContext.xml]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [com.gdpi.dao.BookDao]: Constructor threw exception; nested exception is java.lang.NullPointerException
applicationContext.xml的BookDao注入方式如下<bean id="BookDao" class="com.gdpi.dao.BookDao">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
改成如下代码可以,但每次save当获得一个新的sessionpublic class BookDao extends HibernateDaoSupport {
private Session se; public void save(Book book) {
se=this.getSession();
se.save(book);
}
通过MyEclipse自动生成所需要的包,版本在标签里有。
试过各种方法,包括用sping注入session等,刚接触ssh来做实训,求清晰思路!sessionsshspring3.0struts2.1hibernate3.3

解决方案 »

  1.   

    this.getCurrentSession();获取当前线程的唯一Session。
      

  2.   

    当前线程的唯一Session对象怎么生成的,在dao里?spring里?
      

  3.   

    就在你的dao里面生成,每次调用this.getCurrentSession();的时候会从线程里面拿,你继承了HibernateDaoSupport 并且dao注入了。
    <property name="sessionFactory">
                <ref bean="sessionFactory" />
            </property>
      

  4.   

    就在你的dao里面生成,每次调用this.getCurrentSession();的时候会从线程里面拿,你继承了HibernateDaoSupport 并且dao注入了。
    <property name="sessionFactory">
                <ref bean="sessionFactory" />
            </property>
    没有你说的那个方法
      

  5.   

    就在你的dao里面生成,每次调用this.getCurrentSession();的时候会从线程里面拿,你继承了HibernateDaoSupport 并且dao注入了。
    <property name="sessionFactory">
                <ref bean="sessionFactory" />
            </property>
    没有你说的那个方法

    this.getSessionFactory().getCurrentSession();
      

  6.   

    第一次启动服务器,线程里面没有session,通过什么方式new出这唯一的session
      

  7.   

    getCurentSession的时候,会判断的,如果没有会新建。他会保证你拿得到Session,并且同一个Thread 是同一个。
      

  8.   

    getCurentSession的时候,会判断的,如果没有会新建。他会保证你拿得到Session,并且同一个Thread 是同一个。
    试过了不行才问的
      

  9.   

    getCurentSession的时候,会判断的,如果没有会新建。他会保证你拿得到Session,并且同一个Thread 是同一个。
    试过了不行才问的
    你确定配置是正确的,主要是Spring的注入。
      

  10.   

    上网查了下hibernate.cfg.xml要添加这句
    <property name="current_session_context_class">thread</property>
    Spring要怎么添加
      

  11.   

    用了这个
    <prop key="current_session_context_class">thread</prop>
    或者这个
    <prop key="org.springframework.orm.hibernate3.SpringSessionContext">thread</prop>
    都不行,有没有报错
      

  12.   


    第一句复制错了,应该是<prop key="hibernate.current_session_context_class">thread</prop>
      

  13.   


    第一句复制错了,应该是<prop key="hibernate.current_session_context_class">thread</prop>
    很好,通过自己努力解决了,求分。