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

信息: Configured SessionFactory: null 配置文件没问题。在线等
public void addUser(Object objuect){
Configuration conf = null;
SessionFactory sf = null;
Session session = null;
Transaction tx = null;
try{
//1.读取配置文件
conf = new Configuration().configure();
//2.创建SessionFactory
sf = conf.buildSessionFactory();
//3.打开Session
session = sf.openSession();
//4.开始一个事务
tx = session.beginTransaction();
//5.持久化操作
session.save(objuect);
//6.提交事务
tx.commit();
}catch(HibernateException e){
//6.回滚事务
tx.rollback();
e.printStackTrace();
}finally{
//7.关闭session
session.close();
sf.close();
}

}


---------------------------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">

<!-- Generated by MyEclipse Hibernate Tools.                   -->
<hibernate-configuration>

<session-factory>
<property name="dialect">
org.hibernate.dialect.Oracle11Dialect
</property>
<property name="connection.url">
jdbc:oracle:thin:@localhost:1521:orcl
</property>
<property name="connection.username">useradmin</property>
<property name="connection.password">useradmin</property>
<property name="connection.driver_class">
oracle.jdbc.driver.OracleDriver
</property>
<property name="myeclipse.connection.profile">house</property>
<property name="show_sql">true</property>
<mapping resource="entity/Users.hbm.xml" />

</session-factory>

</hibernate-configuration>


----------------------------User的映射文件----------------------------------------


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.qidian.entity.User" table="users">
<id name="id" type="java.lang.Integer">
<column name="id" />
<generator class="sequence">
<param name="sequence">customer_id_seq</param>
</generator>
</id>
<!-- 用户名 -->
<property name="name" type="String">
<column name="name" length="50" not-null="true" />
</property>
<!-- 用户名密码 -->
<property name="password" type="java.lang.String">
<column name="password" length="50" not-null="true"/>
</property>
<!-- 联系电话 -->
<property name="telep