1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
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;
    }
}