新版本中spring与hibernate整合dao层调用session的方法
2010-12-02 15:24
701 查看
(选自:《Beginning Hibernate》及《spring-framework-3.0.5.RELEASE reference》)
在spring和hibernate整合框架中,session factory被配置为一个spring
bean,以前版本会利用HibernateDaoSupport和HibernateTemplate这两个类,在新版本中可直接利用方法
session factory的getCurrentSession()返回当前事务的session。
如:
Hibernate 3.0.1开始具有有contextual sessions特性,这样hibernate为每个事务管理一个当前session。之前都是spring的相关支持类来实现这个功能。我们推荐使用这种方式。
优点:只依赖于hibernate,不再需要spring相关类。减小入侵性。
(Hibernate 3 has a feature called contextual sessions, wherein
Hibernate itself manages one current
per transaction. This is roughly
equivalent to Spring's synchronization of one Hibernate
per transaction. A corresponding
DAO implementation resembles the following example, based on the plain
Hibernate API:)
Spring的 LocalSessionFactoryBean为其事务策略 支持Hibernate的
SessionFactory.getCurrentSession() 方法, 返回当前spring管理的事务session,
(Spring's LocalSessionFactoryBean supports Hibernate's
SessionFactory.getCurrentSession() method for any Spring transaction strategy, returning
the current Spring-managed transactional Session even with HibernateTransactionManager.)
With your session factory configured as a Spring bean, you can now go on to create DAOs that take
advantage of Hibernate’s functionality. Previous versions of Spring and Hibernate required the use of
the HibernateDaoSupport class and/or HibernateTemplate class to form the basis of your DAOs; however,
recent versions of Spring and Hibernate have eliminated the need for these classes. Hibernate now
supports a getCurrentSession() method on the SessionFactory that returns a Session object that is
associated with the current transaction.
在spring和hibernate整合框架中,session factory被配置为一个spring
bean,以前版本会利用HibernateDaoSupport和HibernateTemplate这两个类,在新版本中可直接利用方法
session factory的getCurrentSession()返回当前事务的session。
如:
1. public List<Paper> getAll() { 2. Session session = this.sessionFactory.getCurrentSession(); 3. List<Paper> papers = (List<Paper>)session.createQuery("from Paper").list(); 4. return papers; 5. }
Hibernate 3.0.1开始具有有contextual sessions特性,这样hibernate为每个事务管理一个当前session。之前都是spring的相关支持类来实现这个功能。我们推荐使用这种方式。
优点:只依赖于hibernate,不再需要spring相关类。减小入侵性。
(Hibernate 3 has a feature called contextual sessions, wherein
Hibernate itself manages one current
Session
per transaction. This is roughly
equivalent to Spring's synchronization of one Hibernate
Session
per transaction. A corresponding
DAO implementation resembles the following example, based on the plain
Hibernate API:)
Spring的 LocalSessionFactoryBean为其事务策略 支持Hibernate的
SessionFactory.getCurrentSession() 方法, 返回当前spring管理的事务session,
(Spring's LocalSessionFactoryBean supports Hibernate's
SessionFactory.getCurrentSession() method for any Spring transaction strategy, returning
the current Spring-managed transactional Session even with HibernateTransactionManager.)
public List<Paper> getAll() { Session session = this.sessionFactory.getCurrentSession(); List<Paper> papers = (List<Paper>)session.createQuery("from Paper").list(); return papers;
With your session factory configured as a Spring bean, you can now go on to create DAOs that take
advantage of Hibernate’s functionality. Previous versions of Spring and Hibernate required the use of
the HibernateDaoSupport class and/or HibernateTemplate class to form the basis of your DAOs; however,
recent versions of Spring and Hibernate have eliminated the need for these classes. Hibernate now
supports a getCurrentSession() method on the SessionFactory that returns a Session object that is
associated with the current transaction.
相关文章推荐
- SSH整合(spingmvc,spring,hibernate)以及Dao层抽取方法
- Spring3与hibernate4整合,sessionFactory.getCurrentSession()方法获取不到Session对象异常解决办法
- 第二次报错:在spring+springMVC+hibernate中,controller调用service层方法或者dao层方法,报错java.lang.NullPointerException
- ssh整合思想 Spring与Hibernate和Struts2的action整合 调用action添加数据库 使用HibernateTemplate的save(entity)方法
- spring和hibernate整合使用getCurrentSession方法
- Spring3.3 整合 Hibernate3、MyBatis3.2 配置多数据源/动态切换数据源 方法
- Spring3.3 整合 Hibernate3、MyBatis3.2 配置多数据源/动态切换数据源 方法
- 在spring,hibernate,struts2框架整合中出现的no session问题
- spring管理hibernate获取session方法
- hibernate与spring整合后通用分页查询方法
- 网络编程 -- RPC实现原理 -- RPC -- 迭代版本V4 -- 远程方法调用 整合 Spring 自动注册
- Spring3.3 整合 Hibernate3、MyBatis3.2 配置多数据源/动态切换数据源 方法
- Spring3.3 整合 Hibernate3、MyBatis3.2 配置多数据源/动态切换数据源 方法
- Spring3.3 整合 Hibernate3、MyBatis3.2 配置多数据源/动态切换数据源 方法
- struts+spring+hibernate整合问题解决方法
- [原创]java WEB学习笔记79:Hibernate学习之路--- 四种对象的状态,session核心方法:save()方法,persist()方法,get() 和 load() 方法,update()方法,saveOrUpdate() 方法,merge() 方法,delete() 方法,evict(),hibernate 调用存储过程,hibernate 与 触发器协同工作
- hibernate 与 spring整合,SessionFactory初始化hibernate,Session数据的增删改查
- Spring+Hibernate整合框架下DAO的数据库访问方法
- JUNIT测试Hibernate和Spring的Dao层方法出现的问题
- 当Dao层继承了HibernateDaoSupport,使用底层SQL语句,session获取的方法,以及解决关联查询no session的问题