Spring集成hibernate时出错了,请高手指点

youc 发布于 2012/03/10 17:14
阅读 299
收藏 0
错误列表
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:57)
给我报的错事session为空了
这是我的utils类
package cn.micblog.utils;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;

/**
 * <p>
 * 文件名: HibernateUtil.java
 * </p>
 * <p>
 * 类职责: 线程范围内的Hibernate Session共享工具类
 * </p>
 * <p>
 * CopyRight 2012 中国矿业大学银川学院
 * </p>
 * @author youc
 * @version 1.0
 * @date 2012-03-08
 */
public class HibernateUtil {

private static Log log = LogFactory.getLog(HibernateUtil.class);

private static SessionFactory sessionFactory;

private static final ThreadLocal<Session> threadSession = new ThreadLocal<Session>();

private static final ThreadLocal<Transaction> threadTransaction = new ThreadLocal<Transaction>();

public HibernateUtil(SessionFactory coreSessionFactory) {
super();
sessionFactory = coreSessionFactory;
log.info("=================HibernateUtil constructor......");
}

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;
}
}

public static void commitTransaction() throws HibernateException {
Transaction tx = threadTransaction.get();
try {
if (tx != null && !tx.wasCommitted() && !tx.wasRolledBack()) {
log.info("###Committing database transaction of this thread.");
tx.commit();
}
threadTransaction.remove();
} catch (HibernateException ex) {
rollbackTransaction();
throw ex;
}
}

public static void rollbackTransaction() throws HibernateException {
Transaction tx = threadTransaction.get();
try {
threadTransaction.set(null);
if (tx != null && !tx.wasCommitted() && !tx.wasRolledBack()) {
log.info("###Tyring to rollback database transaction of this thread.");
tx.rollback();
}
} catch (HibernateException ex) {
throw ex;
} finally {
closeSession();
}
}

}

加载中
返回顶部
顶部