sessionFactory为Null!!!
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()); }}
<?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>