Spring对Hibernate的集成提供了很好的支持,Spring提供了对sessionfactory的初始化,用户不用再关心session的open,close,同时,Spring还提供了灵活的事务声明.
本文通过实例简单讲述如何在Spring中集成Hibernate.
1,将hibernate的配置加入到Spring的配置中(对datasource和sessionfactory进行配置):
- <bean id="dataSource"
- class="org.springframework.jdbc.datasource.DriverManagerDataSource">
- <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />
- <property name="url" value="jdbc:oracle:thin:@localhost:1521:ORCL" />
- <property name="username" value="user" />
- <property name="password" value="pwd" />
- </bean>
- <bean id="mySessionFactory"
- class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
- <property name="dataSource" ref="dataSource" />
- <property name="mappingResources">
- <list>
- <value>com/test/spring/hibernate/Hbtest.hbm.xml
- </value>
- </list>
- </property>
- <property name="hibernateProperties">
- <value>
- hibernate.dialect=org.hibernate.dialect.Oracle10gDialect
- hibernate.show_sql=true
- hibernate.format_sql=true
- hibernate.current_session_context_class=thread
- </value>
- </property>
- </bean>
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" /> <property name="url" value="jdbc:oracle:thin:@localhost:1521:ORCL" /> <property name="username" value="user" /> <property name="password" value="pwd" /> </bean> <bean id="mySessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <property name="mappingResources"> <list> <value>com/test/spring/hibernate/Hbtest.hbm.xml </value> </list> </property> <property name="hibernateProperties"> <value> hibernate.dialect=org.hibernate.dialect.Oracle10gDialect hibernate.show_sql=true hibernate.format_sql=true hibernate.current_session_context_class=thread </value> </property> </bean>Hibernate的配置也可以单独放在hibernate.cfg.xml文件中,然后通过如下的配置指向config文件,这样在spring配置中就不要配置dataSource和其他Hibernate相关的信息了.
- <bean id="mySessionFactory"
- class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
- <property name="configLocation" value="classpath:conf/Hibernate.cfg.xml" />
- </bean>
<bean id="mySessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="configLocation" value="classpath:conf/Hibernate.cfg.xml" /> </bean>2,将sessionfactory注入到DAO中,通过sessionfactory操作Hibernate提供的API:
- <bean id="PlainHibernateDAO" class="com.test.spring.hibernate.PlainHibernateDAO">
- <property name="sessionFactory" ref="mySessionFactory" />
- </bean>
<bean id="PlainHibernateDAO" class="com.test.spring.hibernate.PlainHibernateDAO"> <property name="sessionFactory" ref="mySessionFactory" /> </bean>应用实例:
1,通过继承HibernateDaoSupport 或者自己实例化HibernateTemplate来
使用HibernateTemplate,
HibernateTemplate
会确保当前Hibernate的 Session
对象的正确打开和关闭,并直接参与到事务管理中去,不要用户去管session的打开和关闭.然后直接调用HibernateTemplate
提供的方法.
- public class DAOSupportDAO extends HibernateDaoSupport {
- public void persist2(Hbtest transientInstance) {
- log.debug("persisting Hbtest instance");
- try {
- this.getHibernateTemplate().save(transientInstance);
- log.debug("persist successful");
- } catch (RuntimeException re) {
- log.error("persist failed", re);
- throw re;
- }
- }
- ...
- }
public class DAOSupportDAO extends HibernateDaoSupport { public void persist2(Hbtest transientInstance) { log.debug("persisting Hbtest instance"); try { this.getHibernateTemplate().save(transientInstance); log.debug("persist successful"); } catch (RuntimeException re) { log.error("persist failed", re); throw re; } } ... }2,通过继承HibernateDaoSupport
使用
SessionFactory.
- public class DAOSupportDAO extends HibernateDaoSupport {
- private static final Log log = LogFactory.getLog(DAOSupportDAO.class);
- public void persist(Hbtest transientInstance) {
- log.debug("persisting Hbtest instance");
- try {
- this.getSessionFactory().getCurrentSession().save(transientInstance);
- log.debug("persist successful");
- } catch (RuntimeException re) {
- log.error("persist failed", re);
- throw re;
- }
- }
public class DAOSupportDAO extends HibernateDaoSupport { private static final Log log = LogFactory.getLog(DAOSupportDAO.class); public void persist(Hbtest transientInstance) { log.debug("persisting Hbtest instance"); try { this.getSessionFactory().getCurrentSession().save(transientInstance); log.debug("persist successful"); } catch (RuntimeException re) { log.error("persist failed", re); throw re; } }3,通过注入SessionFactory(推荐做法:好处是不会引入Spring的类)
- public class PlainHibernateDAO {
- private static final Log log = LogFactory.getLog(PlainHibernateDAO.class);
- private SessionFactory sessionFactory;
- public void setSessionFactory(SessionFactory sessionFactory) {
- this.sessionFactory = sessionFactory;
- }
- public void persist(Hbtest transientInstance) {
- log.debug("persisting Hbtest instance");
- try {
- sessionFactory.getCurrentSession().save(transientInstance);
- log.debug("persist successful");
- } catch (RuntimeException re) {
- log.error("persist failed", re);
- throw re;
- }
- }
public class PlainHibernateDAO { private static final Log log = LogFactory.getLog(PlainHibernateDAO.class); private SessionFactory sessionFactory; public void setSessionFactory(SessionFactory sessionFactory) { this.sessionFactory = sessionFactory; } public void persist(Hbtest transientInstance) { log.debug("persisting Hbtest instance"); try { sessionFactory.getCurrentSession().save(transientInstance); log.debug("persist successful"); } catch (RuntimeException re) { log.error("persist failed", re); throw re; } }对于事务的管理:
1,显示的用Hibernate提供的 SessionFactory.getCurrentSession().beginTransaction(),commit,这时候
hibernateProperties要配置hibernate.current_session_context_class=thread
- <SPAN style="COLOR: #000000"> <property name="hibernateProperties">
- <value>
- hibernate.current_session_context_class=thread
- </value></SPAN>
<property name="hibernateProperties">
<value>
hibernate.current_session_context_class=thread
</value>
2,利用Spring的事务声明来做管理:hibernateProperties不能配置hibernate.current_session_context_class=thread
- <tx:advice id="txAdvice" transaction-manager="txManager">
- <tx:attributes>
- <tx:method name="*" propagation="REQUIRED"/>
- </tx:attributes>
- </tx:advice>
- <bean id="txManager"
- class="org.springframework.orm.hibernate3.HibernateTransactionManager">
- <property name="sessionFactory" ref="mySessionFactory" />
- </bean>
- <aop:config>
- <aop:pointcut id="persistOperation"
- expression="execution(* com.test.spring.hibernate.PlainHibernateDAO.*(..))" />
- <aop:advisor advice-ref="txAdvice" pointcut-ref="persistOperation" />
- </aop:config>
<tx:advice id="txAdvice" transaction-manager="txManager"> <tx:attributes> <tx:method name="*" propagation="REQUIRED"/> </tx:attributes> </tx:advice> <bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory" ref="mySessionFactory" /> </bean> <aop:config> <aop:pointcut id="persistOperation" expression="execution(* com.test.spring.hibernate.PlainHibernateDAO.*(..))" /> <aop:advisor advice-ref="txAdvice" pointcut-ref="persistOperation" /> </aop:config>