注意Hibernate4在开发当中的一些改变

注意Hibernate4在开发当中的一些改变

Hibernate4的改动较大只有spring3.1以上版本可以支持,Spring3.1取消了HibernateTemplate,由于Hibernate4的事务管理已经很好了,不用Spring再扩展了。这里简单介绍了hibernate4相对于hibernate3配置时出现的错误,只列举了问题和解决方法,详细原理若是你们感兴趣仍是去本身搜吧,网上不少。 java

  1. Spring3.1去掉了HibernateDaoSupport类。hibernate4须要经过getCurrentSession()获取session。而且设置
    <prop key="hibernate.current_session_context_class">org.springframework.orm.hibernate4.SpringSessionContext</prop>
    (在hibernate3的时候是thread和jta)。 spring

  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对hibernate的事务管理,不管是注解方式仍是配置文件方式统一改成:
    <bean id="txManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager" >  
    <property name="sessionFactory"><ref bean="sessionFactory"/>
    </property> 
    </bean> 缓存

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

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



-------------------------------------------------------------------- ide

找到篇好文章,我以前遇到的问题都在这都能找到。其实出现这些问题的关键就是hibernate4和hibernate3出现了session管理的变更。 单元测试

spring也做出相应的变更.... 测试

错误1:java.lang.NoClassDefFoundError: org/hibernate/cache/CacheProvider ui

缘由:spring的sessionfactory和transactionmanager与支持hibernate3时不一样。

解决:

 

<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">

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

...

</bean>



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

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

</bean>



错误2:java.lang.NoSuchMethodError:org.hibernate.SessionFactory.openSession()Lorg/hibernate/classic/Session

缘由:hibernate4以后,spring31把HibernateDaoSupport去除,包括数据访问都不须要hibernatetemplate,这意味着dao须要改写,直接使用hibernate的session和query接口。

解决:为了改写dao,足足花了一天时间,而后一个个接口进行单元测试,这是蛋疼的一个主要缘由。


错误3:nested exception is org.hibernate.HibernateException: No Session found for current thread

缘由:发现一些bean没法得到当前session,须要把以前一些方法的事务从NOT_SUPPORT提高到required,readonly=true

见https://jira.springsource.org/browse/SPR-9020, http://www.iteye.com/topic/1120924

解决:

 

<tx:advice id="baseServiceAdvice" transaction-manager="transactionManager">

   <tx:attributes>

      <tx:method name="get*" read-only="true" propagation="REQUIRED"/><!--以前是NOT_SUPPORT-->

      <tx:method name="find*" read-only="true" propagation="REQUIRED"/><!--以前是NOT_SUPPORT-->

      <tx:method name="save*" propagation="REQUIRED"/>

      <tx:method name="update*" propagation="REQUIRED"/>

      <tx:method name="remove*" propagation="REQUIRED"/>

      <tx:method name="add*" propagation="REQUIRED"/>

      <!--默认其余方法都是REQUIRED-->

      <tx:method name="*"/>

   </tx:attributes>

</tx:advice>



错误4:与错误3报错相似,java.lang.NoSuchMethodError:org.hibernate.SessionFactory.openSession()Lorg/hibernate/classic/Session;

        at org.springframework.orm.hibernate3.SessionFactoryUtils.doGetSession(SessionFactoryUtils.java:324) [spring-orm-3.1.1.RELEASE.jar:3.1.1.RELEASE]

        at org.springframework.orm.hibernate3.SessionFactoryUtils.getSession(SessionFactoryUtils.java:202) [spring-orm-3.1.1.RELEASE.jar:3.1.1.RELEASE]

        at org.springframework.orm.hibernate3.support.OpenSessionInViewFilter


缘由:由于opensessioninview filter的问题,若是你的配置仍是hibernate3,须要改成hibernate4 

<filter>

    <filter-name>openSessionInViewFilter</filter-name>

   <filter-class>org.springframework.orm.hibernate4.support.OpenSessionInViewFilter</filter-class>

</filter>


--------------------------------------------------------------------

因为Hibernate4已经彻底能够实现事务了, 与Spring3.1中的hibernatedao,hibernateTemplete等有冲突,因此Spring3.1里已经不提供Hibernatedaosupport,HibernateTemplete了,只能用Hibernate原始的方式用session:

        Session session = sessionFactory.openSession();
        Session session = sessionFactory.getCurrentSession();
在basedao里能够用注入的sessionFactory获取session.

注意, 配置事务的时候必须将父类baseServiceImpl也配上,要否则会出现错误:No Session found for currentthread, 之前是不须要的

SessionFactory.getCurrentSession()的后台实现是可拔插的。所以,引入了新的扩展接口 (org.hibernate.context.spi.CurrentSessionContext)和

新的配置参数(hibernate.current_session_context_class),以便对什么是“当前session”的范围和上下文(scope and context)的定义进行拔插。


Spring @Transactional声明式事务管理,”currentSession”的定义为: 当前被 Spring事务管理器 管理的Session,此时应配置:

hibernate.current_session_context_class=org.springframework.orm.hibernate4.SpringSessionContext。


此处必定注意 使用 hibernate4,在不使用OpenSessionInView模式时,在使用getCurrentSession()时会有以下问题: 当有一个方法list 传播行为为Supports,当在另外一个方法getPage()(无事务)调用list方法时会抛出org.hibernate.HibernateException: No Session found for current thread 异常。 这是由于getCurrentSession()在没有session的状况下不会自动建立一个,不知道这是否是Spring3.1实现的bug。 所以最好的解决方案是使用REQUIRED的传播行为

相关文章
相关标签/搜索