📄 hibernateproxy.java
字号:
package com.placard.core.common.hibernate;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;
import com.placard.core.common.exception.BusinessException;
public class HibernateProxy{
private static Log logger = LogFactory.getLog(HibernateProxy.class);
public Object run(HibernateCallBack callBack)throws BusinessException{
Session session = null;
Transaction tx = null;
try {
session = HibernateUtil.getSession();
tx = session.beginTransaction();
Object result = callBack.execute();
tx.commit();
session.flush();
return result;
} catch (HibernateException e) {
try {
tx.rollback();
} catch (HibernateException e1) {
logger.error(this.getClass().toString()+":事务回滚时出错");
throw new BusinessException("事务回滚时出错!");
}
logger.error(this.getClass().toString()+":执行操作出错!");
throw new BusinessException(e.getMessage());
}finally{
try {
HibernateUtil.closeSession();
} catch (HibernateException e) {
logger.error(this.getClass().toString()+":关闭session时出错!");
throw new BusinessException("关闭session时出错!");
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -