首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > Java Web开发 >

HibernateSessionFactory.getSession()空指针错误

2011-12-21 
HibernateSessionFactory.getSession()空指针异常DAO层中Session session HibernateSessionFactory.getS

HibernateSessionFactory.getSession()空指针异常
DAO层中
Session session = HibernateSessionFactory.getSession();一直都是空指针,不知道问题出现在哪儿,貌似,HibernateSessionFactory里面并没有出现问题,应该是我的水平配置文件有问题,文件如下,麻烦,高手帮我查看一下空指针,为什么,会出现。

<?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.0.xsd
  http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
  http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
  <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
  <property name="url" value="jdbc:mysql://127.0.0.1:3306/expense"></property>
  <property name="username" value="root"></property>
  <property name="password" value="888888"></property>
  <property name="maxActive" value="100"></property>
  <property name="maxIdle" value="30"></property>
  <property name="maxWait" value="500"></property>
  <!-- please use AutoCode register edition ,We use XML mode configer the transaction ,the Function will Strong than Now ,thank you !-->
  <property name="defaultAutoCommit" value="true"></property>
</bean>

<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
  <property name="dataSource" ref="dataSource"></property>
  <property name="hibernateProperties">
  <props>

  <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
  <prop key="hibernate.show_sql">true</prop>
  </props>
  </property>

  <property name="mappingResources">
  <list>
  <value>ac/ssh/bean/Expense.hbm.xml</value>
  </list>
  </property>
</bean>

   
   
  <!-- 配置事务管理器 -->  
  <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">  
  <property name="sessionFactory">  
  <ref local="sessionFactory"/>  
  </property>  
  </bean>  
   
  <!-- 配置事务特性 -->  
  <tx:advice id="txAdvice" transaction-manager="transactionManager">  
  <tx:attributes>  
  <tx:method name="*" propagation="REQUIRED"/>  
  <!--  
  <tx:method name="add*" propagation="REQUIRED"/>  
  <tx:method name="del*" propagation="REQUIRED"/>  


  <tx:method name="update*" propagation="REQUIRED"/>  
  <tx:method name="deploy*" propagation="REQUIRED"/>  
  <tx:method name="submit*" propagation="REQUIRED"/>  
  <tx:method name="*" read-only="true"/>  
  -->  
  </tx:attributes>  
  </tx:advice>  
   
  <!-- 配置那些类的方法进行事务管理 -->  
  <aop:config>  
  <aop:pointcut id="allManagerMethod" expression="execution (* com.gdf.crm.manager.*.*(..))"/>  
  <aop:advisor advice-ref="txAdvice" pointcut-ref="allManagerMethod"/>  
  </aop:config>  


<bean id="expenseDao" class="ac.ssh.dao.impl.ExpenseDAOImpl">
  <property name="sessionFactory">
  <ref bean="sessionFactory"/>
  </property>
</bean>

<bean id="expenseService" class="ac.ssh.service.impl.ExpenseServiceImpl">

  <property name="expenseDao" ref="expenseDao"></property>
</bean>

</beans>


[解决办法]
使用Spring的HibernateDaoSupport的getSession方法。
[解决办法]
HibernateSessionFactory 是null 还是 获得的session 是null?而且奇怪的是你都使用了spring 干嘛自己建立了一个sessionFactory的一个工厂?

热点排行