SessionFactory.getCurrentSession() 是 Hibernate 应用获取 Session
的常用方法。在调用该方法时,Hibernate 会从 interface CurrentSessionContext 获取当前的
Session,这是Hibernate 在不同组件中传递 Session 的方法。
CurrentSessionContext 有三个实现,分别是
ThreadLocalSessionContext、JTASessionContext 和
ManagedSessionContext。ThreadLocalSessionContext 将 Session
与当前线程绑定,是使用较多的一种方案;JTASessionContext 将 Session 与 JTA 事务绑定,在 JTA
环境中使用;ManagedSessionContext 使应用可以通过 bind() 和 unbind() 方法控制 Session
的绑定,主要在有 Conversation 的应用中使用(如果使用
ManagedSessionContext,开发人员要做的事情还是很多的)。CurrentSessionContext 实现的选择可以通过
hibernate.current_session_context_class 来配置。
另一种更常见的创建 Session 的方法是 openSession()。openSession() 与
getCurrentSession() 有何不同和关联呢?在 SessionFactory 启动的时候,Hibernate
会根据配置创建相应的 CurrentSessionContext,在 getCurrentSession() 被调用的时候,实际被执行的方法是
CurrentSessionContext.currentSession() 。在 currentSession() 执行时,如果当前
Session 为空,currentSession 会调用 SessionFactory 的 openSession。所以
getCurrentSession() 对于 Java EE 来说是更好的获取 Session 的方法。
再说 ManagedSessionContext,它提供了更灵活的绑定 Session 的方式,但是使用起来去不简单。在 Hibernate
的 CaveatEmptor 实例中有关于使用 ManagedSessionContext 的例子,但更好的选择是使用 Seam
Framework。
分享到:
相关推荐
NULL 博文链接:https://zgdkik.iteye.com/blog/1835667
ssh整合中getCurrentSession()的使用,个人写的练习,备忘用,不建议下载
NULL 博文链接:https://bbxyhaihua.iteye.com/blog/505085
博文链接:https://shaqiang32.iteye.com/blog/201918
hibernate 学习笔记: 了解hibernate的基本概念 配置hbm.xml cfg.xml 快速入门案例3: 从domain-xml-数据库表 ...openSession()和getCurrentSession() 线程局部变量模式 transaction事务 在web项目中开发hibernate
mysql,jdbc,Hibernate,tomcat [mysql+jdbc+jsp+Hibernate3.2+tomcattomcat5.028成功测试-简单] <br> 1 .数据库设计:库mydb 表events <br>EVENT_ID BIGINT(20) NOT NULL AUTOINC EVENT_DATE DATETIME...
Hibernate 修改数据 1.用 HQL方式来更新 在 这里修改 Person 的name 和age 通过 id 标识 Session currentSession = H3Utils.getCurrentSession(); currentSession.beginTransaction(); //创建 HQL String ...
Configuration cfg=new Configuration().configure("hibernate.cfg.xml"); SessionFactory sf=cfg.buildSessionFactory(); Session session=sf.getCurrentSession(); Transaction tx=session.beginTransaction();
实现一个基于hibernate3的基本的dao,实现CRUD; 一行代码完成CRUD,提供多种重载方法详见IBaseDao,但是项目需要基于SSH框架。 ============ 使用方法,以用户添加为例: 1、定义User类,并提供get和set方法。 2、...
9、openSession和getCurrentSession 90 10、拦截器的作用?拦截器和过滤器的区别? 91 11、struts.xml中result的type有哪些类型? 91 12、什么时候用JDBC什么时候用Hibernete; 91 13、hibernate 数据的三个状态 91 ...
一,集成 Spring 与 Hibernate 1,配置SessionFactory 1,配置 ---------------------- applicationContext.xml ------------------------ <!-- 配置SessionFactory(整合Hibernate) --> ...
不带参数的情况下Web.config中一定要有hibernate-configuration节 5.连接是单线程的,在整个httpRequest处理过程中是公用的,缓存在HttpContext context中 see ref NHibernateHelper.getSession... 6.增加log4net演示,...
123.在 hibernate 中 getCurrentSession 和 openSession 的区别是什么? 124.hibernate 实体类必须要有无参构造函数吗?为什么? 十三、Mybatis 125.mybatis 中 #{}和 ${}的区别是什么? 126.mybatis 有几种分页方式...
学生在学习jdbc的时候,会问到怎么调用存储过程,现在将java调用oracle存储过程的示例总结如下
Query query = getHibernateTemplate().getSessionFactory().getCurrentSession().createQuery(sql); query.setFirstResult(pageno); query.setMaxResults(3); //设置每页显示的条数 List result = ...
1)支持三个技术栈simple(jsp,clocksimplejee或默认,s2sh(Struts2,Spring 4, Hibernate 4)和s2shcs2sh(Struts2,Spring 4, Hibernate 4 Criteria) 2)推荐工业级的S2SHC技术栈 ===============0.7.22================...