(选自:《Beginning Hibernate》及《spring-framework-3.0.5.RELEASE reference》)
在spring和hibernate整合框架中,session factory被配置为一个spring bean,以前版本会利用HibernateDaoSupport和HibernateTemplate这两个类,在新版本中可直接利用方法 session factory的getCurrentSession()返回当前事务的session。
如:
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:)
SessionFactory.getCurrentSession() 方法, 返回当前spring管理的事务session,
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.