Hibernate4的改动较大只有spring3.1以上版本能够支持,Spring3.1取消了HibernateTemplate,因为Hibernate4的事务管理已经很好了,不用Spring再扩展了。这里简单介绍了hibernate4相对于hibernate3配置时出现的错误,只列举了问题和解决方法,详细原理如果大家感兴趣还是去自己搜吧,网上很多。

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

  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过滤器。

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

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

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

spring也作出相应的变动....

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

原因: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的传播行为

注意Hibernate4在开发当中的一些改变的更多相关文章

  1. 注意Hibernate4在开发当中的一些改变(转)

    注意Hibernate4在开发当中的一些改变 原文:http://hi.baidu.com/austincao/item/fc9907da3d854e44fa576861 Hibernate4的改动较 ...

  2. Android开发当中Parcelable接口的使用

    本文转载于:http://www.2cto.com/kf/201205/132814.html 本文稍微做了些修改 android提供了一种新的类型:Parcel.本类被用作封装数据的容器,封装后的数 ...

  3. Android开发当中的JavaBean实现

    一般我们在Android开发当中如果会对一些数据类进行解析,那么则需要写出一个JavaBean的类,比如在进行json解析的时候,就需要使用这个类进行数据的处理,下面是我们的JavaBean的模板代码 ...

  4. C#开发分享:如何改变系统鼠标样式

    开发过程中发现需要用到改变鼠标样式(就是光标的样子),但是在网上找了很多资料,都是介绍在程序中使用,我需要的效果时在系统级使用.现在找到了,分享给大家. [DllImport("user32 ...

  5. 关于安卓开发当中通过java自带的HttpURLConnection访问XML的java.io.EOFException问题

    刚接触安卓开发,试着写个小程序熟悉下,就写了天气预报的小程序,通过httpUrlConnection读流的方式来获取网络公共接口提供的天气XML信息.但在建立http连接时一直报java.io.EOF ...

  6. iOS开发笔记--使用blend改变图片颜色

    最近对Core Animation和Core Graphics的内容东西比较感兴趣,自己之前也在这块相对薄弱,趁此机会也想补习一下这块的内容,所以之后几篇可能都会是对CA和CG学习的记录的文章. 在应 ...

  7. ios开发之--字符串局部改变颜色

    改变指定位置字符的颜色,代码如下: NSString *descStr = @"楼主"; NSString *nickStr = [NSString stringWithForma ...

  8. ionic 开发当中,有一些常用的方法。

    在开发项目的时候,有些常用的功能封装到一个类里. 以后要用的话,直接导入过来就可以用了,有一些方法是从网站复制过来的,有些方法是网上复制过来,然后自己修改了一下,标记一下吧!   /**      * ...

  9. 开发当中curl简单使用

    curl是linux上可以发送http请求的命令.当然Postman是一个很好的接口调用管理工具,但在验证一个linux服务器调用另外一个linux服务器API是否可用的场景下,非curl命令莫属. ...

随机推荐

  1. 基础知识(09) -- Spring 概述

    Spring概述-------------------------------------------------------------------------主要内容: 1.Spring是什么 2 ...

  2. asp.net mvc中DropDownList

    asp.net mvc中DropDownList的使用. 下拉列表框 以分为两个部分组成:下拉列表和默认选项 DropDownList扩展方法的各个重载版本基本上都会传递到这个方法上:   publi ...

  3. python相对包导入报“Attempted relative import in non-package”错误

    文章是从stackoverflow翻译过来的,原文地址:Relative imports for the billionth time 本文要在原理上解决  python当中相对包导入出现的问题. 问 ...

  4. PAT甲级1089. Insert or Merge

    PAT甲级1089. Insert or Merge 题意: 根据维基百科: 插入排序迭代,消耗一个输入元素每次重复,并增加排序的输出列表.每次迭代,插入排序从输入数据中删除一个元素,在排序列表中找到 ...

  5. 微信小程序的坑

    虽然官方文档,可以在.json中给页面设置背景颜色,用backgroundColor,但是实际上并不好使,所以设置背景颜色只能在wxss中设置 <import src="../comm ...

  6. mac os颜色拾取工具/软件/器推荐

    软件名:ColorSnappe 目前我用的时1.1.0版本 该工具可以利用alt+command+c快捷键拾取颜色,拾取后可以自动把颜色代码放入剪切板 .我最喜欢它可以生成ios开发的代码,比如选择了 ...

  7. Assembly.Load()方法,Assembly.LoadFrom()方法,Assembly.LoadFile()方法的区别!

    参考: http://www.cnblogs.com/benwu/archive/2009/10/24/1589096.html http://www.cnblogs.com/xuefeng1982/ ...

  8. 三款工作流引擎比较:WWF、netBPM 和 ccflow

    下面将对目前比较主流的三款工作流进行介绍和比较,然后通过三款流程引擎分别设计一个较典型的流程来给大家分别演示这三款创建流程的过程.这三款工作流程引擎分别是 Windows Workflow Found ...

  9. AlphaGo:用机器学习技术古老的围棋游戏掌握AlphaGo: Mastering the ancient game of Go with Machine Learning

    AlphaGo: Mastering the ancient game of Go with Machine Learning Posted by David Silver and Demis Has ...

  10. sqlalchemy: TimeoutError: QueuePool limit of size 5 overflow 10 reached, connection timed out, timeout 30

    mysql建立的连接要及时删除,不然连接池资源耗尽 相关文章参考: http://blog.csdn.net/robinson1988/article/details/4713294 http://b ...