zoukankan      html  css  js  c++  java
  • 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).

  • 相关阅读:
    全排列
    06-联系人管理(xib应用)
    单键模式
    在mac上截屏的几种方式
    05-Tom猫(UIImageView的简单运用)
    04-图片排列切换
    03-图片浏览器(plist的简单应用)
    02-动态创建按钮
    01-实现图片按钮的缩放、动画效果(block的初步应用)
    oracle 监听动态和静态的配置
  • 原文地址:https://www.cnblogs.com/ifzhuanzhu/p/2687233.html
Copyright © 2011-2022 走看看