SSH总结

1. Spring3.X 与 Hibernate4.X

 http://blog.springsource.org/2012/04/06/migrating-to-spring-3-1-and-hibernate-4-1/

 

<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
...
</bean>
<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
     <property name="sessionFactory" ref="sessionFactory"/>
</bean>

 

1、spring3.x 去掉了对 Hibernate 4.x的 HibernateDaoSupport类。Dao层需要Spring注入SessionFactory对象,通过SessionFactory.getCurrentSession()方法或SessionFactory.openSession()来获得Session对象。

通过getCurrentSession()方法获取Session对象时需要设置:

<prop key="hibernate.current_session_context_class">

org.springframework.orm.hibernate4.SpringSessionContext

</prop> 

 

2、缓存设置改为

<prop key="hibernate.cache.provider_class">net.sf.ehcache.hibernate.EhCacheProvider</prop> 

<prop key="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory</prop>

 

3、spring 3.x对 hibernate 4.x的事务管理,不论是注解方式还是配置文件方式统一改为:

<bean id="txManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager" >

<property name="sessionFactory" ref ="sessionFactory"/>

</bean>

sessionFactory 要通过 dataSource 的方式配置,否则出现下面错误:

Error creating bean with name 'dataSource' defined in class path resource [applicationContext.xml]

 

4getCurrentSession()事务会自动关闭,所以在有所jsp页面查询数据都会关闭session。要想在jsp查询数据库需要加入org.springframework.orm.hibernate4.support.OpenSessionInViewFilter过滤器。

 

5hibernate分页出现 ResultSet may only be accessed in a forward direction需要设置hibernate结果集滚动 <prop key="jdbc.use_scrollable_resultset">false</prop>

 

6、当报No Session found for current thread异常时

When openSessionInView filter is not active, getCurrentSession() should be called inside an existing transaction.

So, to ensure creation of transaction you need to make your service method @Transactional as well (or change propagation of your DAO method to REQUIRED).

posted on 2012-09-16 09:48  ifzhuanzhu  阅读(296)  评论(0编辑  收藏  举报