`
langgufu
  • 浏览: 2302060 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

hibernate的session.connection被session.dowork()替代

阅读更多

Hibernate3.3.2版本中getSession().connection()已被弃用,替代方法SessionFactoryUtils.getDataSource(getSessionFactory()).getConnection()

来自类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;
}

Hibernate API中让使用doWork(Work,work),描述如下:

connection()
Deprecated. (scheduled for removal in 4.x). Replacement depends on need; for doing direct JDBC stuff use doWork(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对象的状态详解

    hibernate中session对象的状态详解

    hibernate 执行原生sql的几种方式

    Hibernate的`Session`接口提供了`createSQLQuery()`方法,允许我们直接编写SQL语句。例如: ```java Session session = sessionFactory.openSession(); SQLQuery query = session.createSQLQuery("SELECT * FROM...

    hibernate实现动态表查询的多种解决方案

    session.doWork(connection -> { connection.prepareStatement("ALTER TABLE teacher RENAME TO teacher_" + System.currentTimeMillis()).execute(); }); // 查询操作 List<Teacher> teachers = session....

    hibernate query调用oracle存储过程

    CallableStatement cs = session.doWork(connection -> { CallableStatement stmt = connection.prepareCall(sql); stmt.setString("inputParam", inputValue); stmt.registerOutParameter("outputParam", Oracle...

    hibernate调用存储过程[参考].pdf

    session.doWork(new Work() { public void execute(Connection connection) throws SQLException { CallableStatement cs = connection.prepareCall("{ call getUserList() }"); ResultSet rs = cs.executeQuery...

    基础操作1

    PreparedStatement pstmt = session.doWork(connection -> { PreparedStatement ps = connection.prepareStatement(deleteSql); for (int i = 0; i < ids.size(); i++) { ps.setInt(1, ids.get(i)); ps.addBatch...

    hibernate环境搭建已经对应存储过程调用

    2. **定义CallabledStatement**:在Hibernate中,使用Session的doWork方法或者Session的createCallableStatement方法来执行存储过程。 3. **映射输出参数和结果集**:如果存储过程有输出参数或结果集,你需要定义...

    hibernate调用存储过程.docx

    session.doWork(new Work() { public void execute(Connection connection) throws SQLException { CallableStatement cs = connection.prepareCall("{call pkg_test.get(?, ?)}"); cs.setInt(1, id); cs....

    Hibernate的demo

    通过`Session`对象的`doWork()`方法,我们可以传递一个实现了`Work`接口的实例,然后在其中执行自定义的SQL(包括存储过程)。这使得在需要复杂逻辑或高性能操作时,依然可以利用存储过程的优势,同时保持ORM的便利...

    CREATE PROCEDURE DB2ADMIN.doc

    2. 准备存储过程调用:创建CallableStatement对象,使用`Session`对象的`doWork`方法,传入一个实现了`ConnectionCallback`接口的匿名类,其中的`doInConnection`方法用于执行存储过程调用。 3. 设置参数和执行:在`...

Global site tag (gtag.js) - Google Analytics