日期:2014-05-17  浏览次数:20775 次

sessionFactory返回值为什么是NullPointerException
hibernate3.4+Struts1.3,数据库是SQlServer2008,程序执行到sessionFactory = new Configuration().configure().buildSessionFactory()时返回值sessionFactory是null,
下面的openSession进行不下去了,求高手帮忙看下错误在哪里?
hibernate.cfg.xml
<!DOCTYPE hibernate-configuration PUBLIC

  "-//Hibernate/Hibernate Configuration DTD 3.0//EN"

  "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">


<hibernate-configuration>

  <session-factory>
  <property name="connection.url">jdbc:sqlserver://localhost:1433;DatabaseName=hibernate</property>
  <property name="connection.username">sa</property>
  <property name="connection.password">sa</property>
  <property name="connection.driver_class">com.microsoft.sqlserver.jdbc.SQLServerDriver</property>
  <property name="dialect">org.hibernate.dialect.SQLServerDialect</property>
  <property name="show_sql">true</property>
 
  <mapping resource="person.hbm.xml"/>
 
  </session-factory>

</hibernate-configuration>

------解决方案--------------------
你看後台報什麽錯誤沒? sa密碼是否正確?等等。。

或者,直接加載hibernate.cfg.xml,寫個junit,測試鏈接數據庫是否ok?
------解决方案--------------------
那有可能是你的发布的web工程中的没有发布数据库连接的驱动包。
------解决方案--------------------
你可以编写测试方法,在测试方法中创建sessionFactory,看是否可以创建,如果可以,那就是你配置文件有地方不对
------解决方案--------------------
new Configuration().configure()方法默认是读取src下面的cfg配置文件。。。