Configuration(AnnotationConfiguration)

来源:互联网 发布:网络执法官3.56破解版 编辑:程序博客网 时间:2024/06/10 11:06

Configuration(AnnotationConfiguration)
作用:进行配置信息的管理
目标:用来产生SessionFactory
可以在configure方法中指定hibernate配置文件,默认(不指定)时在classpath下加载hibernate.cfg.xml文件
加载默认的hibernate的配置文件
sessionFactory factory =new AnnotationConfiguration().configure().buildSessionFactory();
加载指定hibernate的配置文件
sessionFactory factory = newnnotationConfiguration().configure(“hibernate.xml”).buildSessionFactory();

SessionFactory
作用:主要用于产生Session的工厂(数据库连接池)
当它产生一个Session时,会从数据库连接池取出一个连接,交给这个Session
Session session= sessionFactory.getCurrentSession();
并且可以通过这个Session取出这个连接
getCurrentSession():表示当前环境没有Session时,则创建一个,否则不用创建
openSession():表示创建一个Session(3.0以后不常用),使用后需要关闭这个Session
两方法的区别:
①、openSession永远是每次都打开一个新的Session,而getCurrentSession不是,是从上下文找、只有当前没有Session时,才创建一个新的Session
②、OpenSession需要手动close,getCurrentSession不需要手动close,事务提交自动close
③、getCurrentSession界定事务边界
所指的上下文是指hibernate配置文件(hibernate.cfg.xml)中的“current_session_context_class”所指的值:(可取值:jta|thread|managed|custom.Class)

0 0
原创粉丝点击