package com.vci.server.base.persistence.dao; import java.sql.Connection; import org.hibernate.HibernateException; import org.hibernate.Session; import org.hibernate.impl.SessionImpl; import org.hibernate.jdbc.JDBCContext; import org.hibernate.jdbc.Work; import com.vci.common.log.ServerWithLog4j; public class HibernateTemplate { /** * * @param callback * @return * @throws HibernateException */ public Object run(HibernateCallback callback) throws HibernateException{ Session session = null; try { session = HibernateSessionFactory.getSession(); Object result = callback.execute(); session.flush(); return result; } catch (Exception e) { //e.printStackTrace(); ServerWithLog4j.logger.error("HibernateTemplate.run", e); throw new RuntimeException(e); } finally { } } public boolean runSql(Work work) throws HibernateException { Session session = null; try { session = HibernateSessionFactory.getSession(); //执行work session.doWork(work); return true; } catch (Exception e) { //e.printStackTrace(); ServerWithLog4j.logger.error("=====HibernateTemplate.runSql====="); ServerWithLog4j.logger.error(e); //ServerWithLog4j.logger.error("Exception StackTrace:", e); ServerWithLog4j.logger.error("=====HibernateTemplate.runSql====="); throw new RuntimeException(e); } finally { } } /** * run HibernateCallbackExt, can using JDBC Connection, is powerfull. * @param callback callback * @return object * @throws HibernateException */ public Object runExt(HibernateCallbackExt callback) throws HibernateException{ Session session = null; try { session = HibernateSessionFactory.getSession(); Object result = callback.execute(getSessionConnection()); session.flush(); return result; } catch (Exception e) { //e.printStackTrace(); ServerWithLog4j.logger.error("=====HibernateTemplate.runExt====="); ServerWithLog4j.logger.error(e); //ServerWithLog4j.logger.error(e.getClass().getName() + ":StackTrace", e); ServerWithLog4j.logger.error("=====HibernateTemplate.runExt====="); throw new RuntimeException(e); } finally { } } /** * 执行 HibernateCallbackExtV5, can using JDBC Connection, is powerfull. * @param HibernateCallbackExtV5 callback 直接使用 DefaultHiberanteCallbackExtV5 的实例 * @return object * @throws HibernateException */ public Object runExtV5(HibernateCallbackExtV5 callback) throws HibernateException{ //Session session = null; try { //session = HibernateSessionFactory.getSession(); Object result = callback.execute(getSessionConnection()); return result; } catch (Exception e) { //e.printStackTrace(); ServerWithLog4j.logger.error("=====HibernateTemplate.runExtV5====="); ServerWithLog4j.logger.error(e); //ServerWithLog4j.logger.error(e.getClass().getName() + ":StackTrace", e); ServerWithLog4j.logger.error("=====HibernateTemplate.runExtV5====="); throw new RuntimeException(e); } finally { } } /** * 返回绑定到当前线程上下文中的Connection对象 * @return 绑定到当前线程上下文中的Connection对象 */ public java.sql.Connection getSessionConnection(){ Session session = HibernateSessionFactory.getSession(); SessionImpl sessionImpl = (SessionImpl)session; JDBCContext jdbcContext = sessionImpl.getJDBCContext(); Connection connection = jdbcContext.getConnectionManager().getConnection(); return connection; } }