关于hibernate.cfg.xml,请高手指点

chufd 2005-12-04 08:46:57
我用HibernateUtil做一个swing测试,在eclipse坏境下可以,为什么打为jar或exe文件就不行,我想问题出在cfg.configure(CONFIG_FILE_LOCATION)这里,打包后找不到 hibernate.cfg.xml文件????

import net.sf.hibernate.HibernateException;
import net.sf.hibernate.Session;
import net.sf.hibernate.Transaction;
import net.sf.hibernate.cfg.Configuration;

public class HibernateUtil {

private static String CONFIG_FILE_LOCATION = "/hibernate.cfg.xml";

/**//** Holds a single instance of Session */
private static final ThreadLocal threadLocal = new ThreadLocal();

private static final ThreadLocal threadTransaction = new ThreadLocal();

/**//** The single instance of hibernate configuration */
private static final Configuration cfg = new Configuration();

/**//** The single instance of hibernate SessionFactory */
private static net.sf.hibernate.SessionFactory sessionFactory;

/**//**
* Returns the ThreadLocal Session instance. Lazy initialize the
* <code>SessionFactory</code> if needed.
*
* @return Session
* @throws HibernateException
*/
public static Session getSession() throws HibernateException {
Session session = (Session) threadLocal.get();

if (session == null) {
if (sessionFactory == null) {
try {
cfg.configure(CONFIG_FILE_LOCATION);
sessionFactory = cfg.buildSessionFactory();
} catch (Exception e) {
System.err.println("%%%% Error Creating SessionFactory %%%%");
e.printStackTrace();
}
}
session = sessionFactory.openSession();
threadLocal.set(session);
}
return session;
}

/**//**
* Close the single hibernate session instance.
*
* @throws HibernateException
*/
public static void closeSession() throws HibernateException {
Session session = (Session) threadLocal.get();
threadLocal.set(null);
if (session != null) {
session.close();
}
}

/**//**
* Default constructor.
*/
private HibernateUtil() {
}

public static void beginTransaction() throws HibernateException {
Transaction tx = (Transaction) threadTransaction.get();
try {
if (tx == null) {
tx = getSession().beginTransaction();
threadTransaction.set(tx);
}
} catch (HibernateException ex) {
throw new HibernateException(ex.toString());
}
}

public static void commitTransaction() throws HibernateException {
Transaction tx = (Transaction) threadTransaction.get();
try {
if (tx != null && !tx.wasCommitted() && !tx.wasRolledBack())
tx.commit();
threadTransaction.set(null);
} catch (HibernateException ex) {
rollbackTransaction();
throw new HibernateException(ex.toString());
}
}

public static void rollbackTransaction() throws HibernateException {
Transaction tx = (Transaction) threadTransaction.get();
try {
threadTransaction.set(null);
if (tx != null && !tx.wasCommitted() && !tx.wasRolledBack()) {
tx.rollback();
}
} catch (HibernateException ex) {
throw new HibernateException(ex.toString());
} finally {
closeSession();
}
}
}
...全文
88 点赞 收藏 1
写回复
1 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
Runningboy007 2005-12-05
学习。顶
回复
相关推荐
发帖
Java EE
创建于2007-09-28

6.6w+

社区成员

J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
申请成为版主
帖子事件
创建了帖子
2005-12-04 08:46
社区公告
暂无公告