标题: Spring集成hibernate出错了。。。。 [打印本页] 作者: youc3576@qq.com 时间: 2012-3-10 18:08 标题: Spring集成hibernate出错了。。。。 帮忙看看
谢谢
Exception in thread "main" java.lang.NullPointerException
at cn.micblog.utils.HibernateUtil.currentSession(HibernateUtil.java:44)
at cn.micblog.dao.BaseBO.currentSession(BaseBO.java:18)
at cn.micblog.dao.BaseBO.save(BaseBO.java:48)
at cn.micblog.dao.impl.MessageDaoImpl.save(MessageDaoImpl.java:16)
at cn.micblog.TestDao.UserDAOTest.main(UserDAOTest.java:58)
我的hibernateutils类是这样的
package cn.micblog.utils;
public static Session currentSession() throws HibernateException {
Session s = threadSession.get();
// Open a new Session, if this Thread has none yet
if (s == null) {
s = sessionFactory.openSession();
log.info("###Opening new Session for this thread:");
threadSession.set(s);
} else {
log.info("###Session was existed:");
}
return s;
}
public static void closeSession() throws HibernateException {
Session s = threadSession.get();
threadSession.remove();
if (s != null) {
log.info("###Closing Session of this thread. ");
s.close();
}
}
public static void beginTransaction() throws HibernateException {
Transaction tx = threadTransaction.get();
try {
if (tx == null) {
tx = currentSession().beginTransaction();
log.info("###Starting new database transaction in this thread:");
threadTransaction.set(tx);
} else {
log.info("###Tx was existed:");
}
} catch (HibernateException ex) {
throw ex;
}
}