Configuration取配置文件 以及取得Session的两种方法的区别

在得到SessionFactory的时候要先取得配置文件:Hibernate.cfg.xml文件 这时有Configuration类得到的:

                    new AnnotationConfiguration().configure().buildSessionFactory();

configure()方法自动会在classpath目录下找HIbernate.cfg.xml文件 ,当文件是别的名的时候就搜不到了这时要:

                   new AnnotationConfiguration().configure("hibernate.xml").buildSessionFactory();


 区别:
   openSession():永远打开新的session 需要close()
   getCurrentSession(): 从上下文找在session没有提交前(session.getTransaction().commit()),打开的都是旧的session 用途: 界定事务边界 事务提交自动close(),需要配置上下文;<property name="current_session_context_class">thread</property>

不可混用

猜你喜欢

转载自wwwzchen.iteye.com/blog/1068298
今日推荐