hibernate的session对象

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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值