Hibernate Session 获取connection
由于最近一个项目要用到一条辅助的SQL ,hibernate里面的SQLQuery API 总的SQL语句不能包含 : 冒号, 固放弃Hibernate, 直接使用JDBC.
Hibernate3.3.2版本中getSession().connection()已被弃用,替代方法
SessionFactoryUtils.getDataSource(getSessionFactory()).getConnection()
原有方法
getSession().connection()
来自类org.springframework.orm.hibernate3.SessionFactoryUtils
例子:
java.sql.Connection c = null; java.sql.PreparedStatement ps = null; java.sql.ResultSet rs = null; public List method(String sql) { List ret = new ArrayList(); try { c = SessionFactoryUtils.getDataSource(getSessionFactory()).getConnection(); ps = c.prepareStatement(sql); rs = ps.executeQuery(); while(rs.next()) { ..... } ret.add(ro); } } catch (Exception e) { e.printStackTrace(); } finally { close(); } return ret; }
我的demo
/** * * * @author mjorcen * @email [email protected] * @dateTime Jul 4, 2014 5:43:05 PM * @version 1 * @throws SQLException */ private void getTop() throws SQLException { StringBuilder sb = new StringBuilder(); sb.append("SELECT X.COMMENT_ID, X.TOTAL_NO, X.ORDER_NO, IFNULL(X.ORDER_NO / X.TOTAL_NO, 1) AS PERCENTAGE_NO "); sb.append("FROM (SELECT @rowNum := @rowNum + 1 AS ORDER_NO, @rowCount AS TOTAL_NO, t.COMMENT_ID, t.COMMENT_SPENDING "); sb.append(" FROM T_SD_COMMENT t, (SELECT @rowNum := 0 "); sb.append(" ) b, (SELECT @rowCount := ( "); sb.append(" SELECT COUNT(*) "); sb.append(" FROM T_SD_COMMENT "); sb.append(" WHERE to_days(CREATE_TIME) >= to_days(now()) "); sb.append(" ) "); sb.append(" ) c "); sb.append(" WHERE to_days(t.CREATE_TIME) >= to_days(now()) ORDER BY t.COMMENT_SPENDING ,t.COMMENT_ID "); sb.append(" ) X "); sb.append("WHERE X.COMMENT_ID = ? "); Connection c = this.commentService.getSession().connection(); PreparedStatement ps = c.prepareStatement(sb.toString()); ps.setInt(1, this.paramComment.getCommentId()); ResultSet rs = ps.executeQuery(); if (rs.next()) { System.out.println("COMMENT_ID = " + rs.getInt("COMMENT_ID")); System.out.println("TOTAL_NO = " + rs.getInt("TOTAL_NO")); System.out.println("ORDER_NO = " + rs.getInt("ORDER_NO")); System.out.println("PERCENTAGE_NO = " + rs.getDouble("PERCENTAGE_NO")); } }
Hibernate API中让使用doWork(Work,work),描述如下:
connection()
Deprecated. (scheduled for removal in 4.x). Replacement depends on need; for doing direct JDBC stuff usedoWork(org.hibernate.jdbc.Work)
; for opening a ‘temporary Session‘ use (TBD).
Work接口的execute()方法用于执行直接通过JDBC API来访问数据库的操作:
public interface Work { // 直接通过JDBC API来访问数据库的操作 public void execute(Connection connection) throws SQLException; }
Session的doWork(Work work)方法用于执行Work对象指定的操作,即调用Work对象的execute()方法。
Session会把当前使用的数据库连接传给execute()方法。
过程如下:
Transaction tx = session.beginTransaction(); // 定义一个匿名类,实现了Work接口 Work work = new Work() { public void execute(Connection connection) throws SQLException { // 通过JDBC API执行用于批量更新的SQL语句 PreparedStatement stmt = connection .prepareStatement("update CUSTOMERS set AGE=AGE+1 " + "where AGE>0 "); stmt.executeUpdate(); } };
执行work
session.doWork(work); tx.commit();
当通过JDBC API中的PreparedStatement接口来执行SQL语句时,SQL语句中涉及到的数据不会被加载到Session的缓存中,因此不会占用内存空间。
Hibernate Session 获取connection