在dao数据访问层操作数据库时用同一个Session,以前JDBC用构造器获取的
Java code?public class BookDao extends HibernateDaoSupport { private Session se; public BookDao(){ se=this.getSession(); } public void save(Book book) { se.save(book); }
整合SSH后报这类错误
<SPAN style="COLOR: #ff0000"> org.springframework.beans.factory.BeanCreationException: </SPAN>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当获得一个新的session
public class BookDao extends HibernateDaoSupport { private Session se; public void save(Book book) { se=this.getSession(); se.save(book); }
通过MyEclipse自动生成所需要的包,版本在标签里有。
<prop key="current_session_context_class">thread</prop> 或者这个 <prop key="hibernate.current_session_context_class">thread</prop>
|