在得到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>
不可混用
Hibernate配置与Session使用
本文介绍了Hibernate中SessionFactory的配置方法,特别是如何指定不同的配置文件名称。同时对比了openSession()和getCurrentSession()两种获取Session的方法及其应用场景。
1万+

被折叠的 条评论
为什么被折叠?



