hibernate的session对象

来源:互联网 发布:伤逝 叶倩文 知乎 编辑:程序博客网 时间:2024/04/30 07:04

hibernate中
session对象与connection对象是多对一得关系

一个session对应一个connection;
一个connection对应N个session对象。

如何获得session

从sessionFactory中,通过openSession();getCurrentSession();获得 。
两者的区别:
getCurrentSession()返回的Session 自动关闭事务
openSession()返回的Session需要手动关闭事务

openSession()每次拿出一个新的Session;getCurrentSession()返回现有Session(单例模式).

其中getCurrentSession();需要在hibernate。cfg.xml文件中中进行配置
本地事务<propertyname="hibernate.current_session_context_class">thread</property>
全局事务

`<propertyname="hibernate.current_session_context_class">jta</property>`

全局事务:资源管理器管理和协调的事务,可以跨越多个数据库和进程。

本地事务:在单个 EIS 或数据库的本地并且限制在单个进程内的事务。本地事务不涉及多个数据来源。


在hibernate中,session对象有以下方法:
save();
update();
delete();
createquery();

dowork();


在hibernate4中官方推荐使用Session doWork()方法进行jdbc操作


Hibernate3.3.2版本中getSession().connection()已被弃用


public interface Work {//Execute the discrete work encapsulated by this work instance using the supplied connection.//@param connection The connection on which to perform the work.// @throws SQLException Thrown during execution of the underlying JDBC interaction.// @throws HibernateException Generally indicates a wrapped SQLException.public void execute(Connection connection) throws SQLException{} //在execute()中进行JDBC操作
0 0
原创粉丝点击