sessionFactory 为null
场景:sessionFactory为Null!解决方法
sessionFactory为Null!!!
hibernate.cfg.xml:
错误如下:
2011-09-30 00:56:19,067 WARN [org.hibernate.internal.util.xml.DTDEntityResolver] - HHH000223: Recognized obsolete hibernate namespace http://hibernate.sourceforge.net/. Use namespace http://www.hibernate.org/dtd/ instead. Refer to Hibernate 3.6 Migration Guide!
2011-09-30 00:56:19,161 WARN [org.hibernate.internal.util.xml.DTDEntityResolver] - HHH000223: Recognized obsolete hibernate namespace http://hibernate.sourceforge.net/. Use namespace http://www.hibernate.org/dtd/ instead. Refer to Hibernate 3.6 Migration Guide!
Exception in thread "main" java.lang.NullPointerException
at com.choiwan.UserDAO.insert(UserDAO.java:26)
at com.choiwan.HibernateDemo.main(HibernateDemo.java:22)
------解决方案--------------------
你这 Configuration config=new Configuration().configure();找到hibernate.cfg.xml这个文件了吗?
sessionFactory为Null!!!
- Java code
package com.choiwan; import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration; public class HibernateDemo { public static void main(String args[]) { //Configuration负责管理Hibernate配置消息 Configuration config=new Configuration().configure(); //根据config建立SessionFactory //SessionFactory将用于建立 Session //@SuppressWarnings("deprecation") SessionFactory sessionFactory=config.buildSessionFactory(); IUserDAO userDAO=new UserDAO(sessionFactory); User user=new User(); user.setName("caterpillar"); user.setAge(new Integer(22)); userDAO.insert(user); user=userDAO.find(new Integer(1)); System.out.println("name:"+user.getName()); } }
hibernate.cfg.xml:
- XML code
<?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> <property name="show_sql"> true </property> <property name="dialect"> org.hibernate.dialect.MySQLDialect </property> <property name="connection.driver_class"> com.mysql.jdbc.Driver </property> <property name="connection.url"> jdbc:mysql://localhost/demo </property> <property name="connection.username"> root </property> <property name="connection.password"> 4131025 </property> <!-- 以下设置对象与数据库表格的映射文件 --> <mapping resource="com/choiwan/User.hbm.xml"/> </session-factory> </hibernate-configuration>
错误如下:
2011-09-30 00:56:19,067 WARN [org.hibernate.internal.util.xml.DTDEntityResolver] - HHH000223: Recognized obsolete hibernate namespace http://hibernate.sourceforge.net/. Use namespace http://www.hibernate.org/dtd/ instead. Refer to Hibernate 3.6 Migration Guide!
2011-09-30 00:56:19,161 WARN [org.hibernate.internal.util.xml.DTDEntityResolver] - HHH000223: Recognized obsolete hibernate namespace http://hibernate.sourceforge.net/. Use namespace http://www.hibernate.org/dtd/ instead. Refer to Hibernate 3.6 Migration Guide!
Exception in thread "main" java.lang.NullPointerException
at com.choiwan.UserDAO.insert(UserDAO.java:26)
at com.choiwan.HibernateDemo.main(HibernateDemo.java:22)
------解决方案--------------------
你这 Configuration config=new Configuration().configure();找到hibernate.cfg.xml这个文件了吗?