swing下用hibernate.cfg.xml的问题?请高手指点,在线等!!!!

chufd 2005-12-04 08:56:09
我用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();
}
}
}
...全文
100 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
bovy 2005-12-22
  • 打赏
  • 举报
回复
需要把 hibernate.cfg.xml 放在ClassPath 中,你在打jar包的时候需要在MAINFEST.MF文件中制定hibernate.cfg.xml 存放的路径.

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧