求解答为什么Dao中总是获得不到SESSION

yoyoyo0 2013-05-22 12:35:06

import java.util.List;

import org.hibernate.Session;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.ssh.dao.ShowSecretDao;
import com.ssh.entity.Secret;

public class ShowSecretDaoImpl extends HibernateDaoSupport implements
ShowSecretDao {
Session s = super.getSession(true);


public List<Secret> searchSecret() {

String hql = "from Secret";
@SuppressWarnings("unchecked")
List<Secret> secretList = super.getHibernateTemplate().find(hql);





return secretList;
}

public void addSecret(Secret secret) {
//getSessionFactory().openSession().save(secret);//这个方法可以成功
s.save(secret);//这个方法不成功
}

}


如上述的dao中,//getSessionFactory().openSession().save(secret); 这个能执行插入数据

但是如果使用s.save(secret);//就会报错,NullPointerException



错误详细信息:


Could not instantiate bean class [com.ssh.dao.impl.ShowSecretDaoImpl]: Constructor threw exception; nested exception is java.lang.NullPointerException
Error creating bean with name 'showSecretDao' defined in file [D:\eclipse\dev\workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\mysecret_ssh\WEB-INF\classes\applicationContext.xml]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [com.ssh.dao.impl.ShowSecretDaoImpl]: Constructor threw exception; nested exception is java.lang.NullPointerException
Error creating bean with name 'showSecretService' defined in file [D:\eclipse\dev\workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\mysecret_ssh\WEB-INF\classes\applicationContext.xml]: Cannot resolve reference to bean 'showSecretDao' while setting bean property 'showSecretDao'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'showSecretDao' defined in file [D:\eclipse\dev\workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\mysecret_ssh\WEB-INF\classes\applicationContext.xml]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [com.ssh.dao.impl.ShowSecretDaoImpl]: Constructor threw exception; nested exception is java.lang.NullPointerException
Unable to instantiate Action, com.ssh.action.ShowSecretAction, defined for 'show' in namespace '/'Error creating bean with name 'showSecretService' defined in file [D:\eclipse\dev\workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\mysecret_ssh\WEB-INF\classes\applicationContext.xml]: Cannot resolve reference to bean 'showSecretDao' while setting bean property 'showSecretDao'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'showSecretDao' defined in file [D:\eclipse\dev\workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\mysecret_ssh\WEB-INF\classes\applicationContext.xml]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [com.ssh.dao.impl.ShowSecretDaoImpl]: Constructor threw exception; nested exception is java.lang.NullPointerException




给大家看下我的Spring-application.xml 和hibernate.cfg.xml



Application.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">


<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="configLocation">
<value>WEB-INF/hibernate.cfg.xml </value>
</property>
</bean>

<!-- action -->
<bean id="showSecretAction" scope="prototype"
class="com.ssh.action.ShowSecretAction">
<property name="showSecretService" ref="showSecretService"></property>
</bean>

<!-- service -->

<bean id="showSecretService" scope="prototype"
class="com.ssh.service.impl.ShowSecretServiceImpl">
<property name="showSecretDao" ref="showSecretDao"></property>
</bean>


<!-- dao -->
<bean id="showSecretDao" scope="prototype"
class="com.ssh.dao.impl.ShowSecretDaoImpl">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>




</beans>







Hibernate


<?xml version="1.0" encoding="UTF-8"?>  
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>

<!-- 显示实际操作数据库时的SQL -->

<property name="show_sql">true</property>

<!-- SQL方言,这边设定的是MySQL -->

<property name="dialect">org.hibernate.dialect.MySQLDialect</property>

<!--驱动程序,在后续的章节中将讲述mysql、sqlserver和Oracle数据库的配置 -->

<property name="connection.driver_class">com.mysql.jdbc.Driver</property>

<!-- JDBC URL -->

<property name="connection.url">jdbc:mysql://*********:3306/secret</property>


<property name="connection.useUnicode">true</property>
<property name="connection.characterEncoding">UTF-8</property>
<!-- 数据库用户名 -->

<property name="connection.username">root</property>

<!-- 数据库密码 -->

<property name="connection.password">*******</property>

<property name="c3p0.min_size">5</property>

<property name="c3p0.max_size">20</property>

<property name="c3p0.timeout">1800</property>

<property name="c3p0.max_statements">50</property>

<!-- 对象与数据库表格映像文件 -->

<mapping resource="com/ssh/entity/secret.hbm.xml" />


</session-factory>

</hibernate-configuration>




貌似数据库连接上是没什么问题,因为试用getSession的方式能成功插入数据,

求解答。
...全文
175 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
william_yao 2013-05-22
  • 打赏
  • 举报
回复
你继承了HibernateDaoSupport,最好也要把HibernateDaoSupport这个代码帖出来啊。你的super.getSession();看看是不是空嘛。打印下喽。应该是注入的时候没成功。
yoyoyo0 2013-05-22
  • 打赏
  • 举报
回复
引用 9 楼 qiuqiupeng 的回复:
java文件: import org.springframework.orm.hibernate3.support.HibernateDaoSupport; 配置文件: <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> <property name="configLocation"> <value>WEB-INF/hibernate.cfg.xml </value> </property> </bean> 另外: hibernate4不推荐使用Hibernate模板来实现持久化!
请问应该如何解决呢,我觉得您说的是有道理的,就是持久化出现了问题
qiuqiupeng 2013-05-22
  • 打赏
  • 举报
回复
java文件: import org.springframework.orm.hibernate3.support.HibernateDaoSupport; 配置文件: <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> <property name="configLocation"> <value>WEB-INF/hibernate.cfg.xml </value> </property> </bean> 另外: hibernate4不推荐使用Hibernate模板来实现持久化!
yoyoyo0 2013-05-22
  • 打赏
  • 举报
回复
引用 6 楼 zslinyuanwz0213 的回复:
既然 继承了 HibernateDaoSupport  为什么还要自己 拿session难道 你要自己做事务吗直接 getHibernateTemplate().save(HQL) getSessionFactory().openSession().save(secret); 想不通 还要这个 有什么用
因为用getHibernateTemplate().save(HQL) 这种方式 是会报错的 NullPointerException
wyx100 2013-05-22
  • 打赏
  • 举报
回复
回复于: 2013-05-22 12:55:37 你继承了HibernateDaoSupport,最好也要把HibernateDaoSupport这个代码帖出来啊。你的super.getSession();看看是不是空嘛。打印下喽。应该是注入的时候没成功。
太灰浪 2013-05-22
  • 打赏
  • 举报
回复
既然 继承了 HibernateDaoSupport  为什么还要自己 拿session难道 你要自己做事务吗直接 getHibernateTemplate().save(HQL) getSessionFactory().openSession().save(secret); 想不通 还要这个 有什么用
dracularking 2013-05-22
  • 打赏
  • 举报
回复
这种方式 Session s = super.getSession(true); 应该是先被spring注入,后才被置空了的吧
yoyoyo0 2013-05-22
  • 打赏
  • 举报
回复
引用 2 楼 peng_hao1988 的回复:
既然都实现HibernateDaoSupport 这个类了,干嘛还要通过session来保持数据? 通过HibernateTemplate类增、删、改、查吧,至于为什么报空你去父类的getSession(boolean f)这个方法中去看吧!
super.getHibernateTemplate().find(hql) 这种方式也是报空指针异常的。
yoyoyo0 2013-05-22
  • 打赏
  • 举报
回复
引用 1 楼 yzw19932010 的回复:
你继承了HibernateDaoSupport,最好也要把HibernateDaoSupport这个代码帖出来啊。你的super.getSession();看看是不是空嘛。打印下喽。应该是注入的时候没成功。
HibernateDaoSupport 是系统的类啊。这不是hibernate提供的嘛。我自己没写过这个 类。
桃园闲人 2013-05-22
  • 打赏
  • 举报
回复
既然都实现HibernateDaoSupport 这个类了,干嘛还要通过session来保持数据? 通过HibernateTemplate类增、删、改、查吧,至于为什么报空你去父类的getSession(boolean f)这个方法中去看吧!

81,092

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧