为何找不到zpdb.hibernate.cfg.xml???
我是利用Hibernate框架,写了一个测试程序,我把zpdb.hibernate.cfg.xml文件与类文件放在一个目录下,可系统报错找不到这个配置文件,请高手帮忙看看~~!程序如下:
package com.mytest.hibernate;
import net.sf.hibernate.HibernateException;
import net.sf.hibernate.Session;
import net.sf.hibernate.SessionFactory;
import net.sf.hibernate.Transaction;
import net.sf.hibernate.cfg.Configuration;
import net.sf.hibernate.cfg.Mappings;
import java.io.Serializable;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Hashtable;
import java.util.Map;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
public class HibernateUtil{
private static SessionFactory sessionFactory;
private static Map persistClassMap;
public static void init(){
persistClassMap=new Hashtable();
try {
Configuration conf=new Configuration();
sessionFactory=conf.configure("zpdb.hibernate.cfg.xml").buildSessionFactory();
} catch (HibernateException e) {
throw new RuntimeException("严重错误!加载Hibernate配置文件失败,原因是"+e.getMessage());
}
}
private static final ThreadLocal sessions=new ThreadLocal();
protected static Session getSession(boolean newSession) throws HibernateException{
if(newSession)
return sessionFactory.openSession();
Session s=(Session)sessions.get();
if(s==null){
s=sessionFactory.openSession();
sessions.set(s);
}
return s;
}
protected static void closeSession(){
Session s=(Session)sessions.get();
sessions.set(null);
if (s != null) {
try {
s.close();
} catch (HibernateException e) {
e.printStackTrace();
}
}
}
protected static void closeSession(Session s){
if (s != null)
try {
s.close();
} catch (HibernateException e) {
e.printStackTrace();
}
}
protected static void rollbackTransaction(Transaction tx){
if(tx!=null)
try {
tx.rollback();
} catch (HibernateException e) {
e.printStackTrace();
}
}
public static Connection getConnection(boolean newSession) throws HibernateException{
if(newSession)
return sessionFactory.openSession().connection();
Session s=(Session)sessions.get();
if(s==null){
s=sessionFactory.openSession();
sessions.set(s);
}
return s.connection();
}
public static void closeConnection(ResultSet rs,Statement stmt,Connection conn){
if(conn!=null){
try {
if(rs!=null)
rs.close();
if(stmt!=null)
stmt.close();
conn.close();
closeSession();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
public void addUser(UserPo user) throws Exception{
if( user == null )
throw new NullPointerException();
HibernateUtil.init();
Transaction tx = null;
Session s = null;
try{
s = this.getSession(false);
tx = s.beginTransaction();
s.save(user);
tx.commit();
}
catch(HibernateException e){
rollbackTransaction(tx);
}finally{
closeSession();
}
}
}