Hibernate入门学习(5)----Session和Transaction

1、Hibernate的执行流程

1.Configuration对象:配置文件对象,读取hibernate配置文件xxx.cfg.xml

2.SessionFactory对象:读取对象/关系映射文件 xxx.hbm.xml

3.session对象:数据库链接对象,获得之后可以操作数据库。sessionde 各种方法如:save(),update(),delete(),createQuery()等。

4.Transaction:使用session操作数据库需要开启的事务 

结合代码来看看hibernate的执行流程:

package onetest;

import java.util.Date;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import one.Students;

public class StudentsTest {
    private SessionFactory sessionFactory;
    private Session session;
    private Transaction transaction;

        @Before
        public void init(){
            //创建配置对象
            Configuration config = new Configuration().configure();
            //创建服务注册对象
            ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings(config.getProperties()).buildServiceRegistry();
            //创建会话工厂对象
            sessionFactory = config.buildSessionFactory(serviceRegistry);
            //创建会话对象
            session = sessionFactory.openSession();
            //开启事务
            transaction = session.beginTransaction();
        }
        @After
        public void destory(){
            //提交事务
            transaction.commit();
            //关闭会话
            session.close();
            //关闭会话工厂
            sessionFactory.close();
        }
        @Test
        public void testStudents(){
            //生成学生对象
            Students s = new Students(1,"jinjin","男",new Date(),"山科");
            session.save(s);//保存对象到数据库

        }
}
上述代码中,先运行Before注解--->Test注解--->After注解,所以和上图中的流程是一样的。

2、Transaction

   ·hibernate对数据的操作都是封装在事务当中,并且默认是非自动提交的方式。所以用session保存对象时,如果不开启事务,并且手工提交事务,对象并不会真正保存在数据库中。

  ·如果想让hibernate想jdbc那样自动提交事务,必须调用session对象的doWork()方法,活得jdbc的connection后,设置其为自动提交事务模式。(注意:通常并不推荐这样做)
//自动提交事务的情况
 @Test
 public void testSaveStudents(){
  Stusdents s= new Students(1,"ZSF",new Date(),"wudang");
  session.doWork(new Work(){
    @Override
    public void execute(Connection connection) throws SQLException{
      connection.setAutoCommit(true);
    }
  })

  session.save(s);//保存对象进入数据库
  session.flush();//强制发出SQL语句(通过SQL写入SQL)
 }

3、Session

如何获得session对象?

(1)openSessionion
(2)getCurrentSession


如果使用getCurrentSession需要在hibernate.cfg.xml文件中进行配置:
如果是本地事务(jdbc事务)(一般都是本地事务)
<property name="hibernate.current_session_context_class">thread</property>
如果是全局事务(jta事务)
<property name="hibernate.current_session_context_class">jta</property>

openSession 每次使用都是打开一个新的session,使用完需要调用close方法关闭session;
getCurrentSession 是获取当前session对象,连续使用多次时,得到的session都是同一个对象,这就是与openSession的区别之一 ;

一般在实际开发中,往往使用getCurrentSession多,因为一般是处理同一个事务,所以在一般情况下比较少使用openSession; 

openSession和getCurrentSession的区别:

1.openSession:每次使用需要手动关闭session,不会自动关闭。cfg.xml文件无需其他配置
2.getCurrentSession:每次使用不需要手动关闭session,会自动关闭。cfg.xml文件需其他配置
3.当你启动hibernate的时候,hibernate就初始化了一个connection对象放在你的数据库连接池里面了。如果你第一次调用openSession的时候,hibernate直接就把连接池里面的connection对象给你了,但是如果你没有关闭session,那么这个connection对象就没有被释放,所以当你再次调用openSession的时候,hibernate就会创建一个新的connection对象,如果一直这样,连接池就溢出了
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值