Hibernate4的改动较大只有spring3.1以上版本能够支持,Spring3.1取消了HibernateTemplate,因为Hibernate4的事务管理已经很好了,不用Spring再扩展了。这里简单介绍了hibernate4相对于hibernate3配置时出现的错误,只列举了问题和解决方法,详细原理如果大家感兴趣还是去自己搜吧,网上很多。
-
Spring3.1去掉了HibernateDaoSupport类。hibernate4需要通过getCurrentSession()获取session。并且设置
<prop key="hibernate.current_session_context_class">org.springframework.orm.hibernate4.SpringSessionContext</prop>
(在hibernate3的时候是thread和jta)。 -
缓存设置改为<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> -
Spring对hibernate的事务管理,不论是注解方式还是配置文件方式统一改为:
<bean id="txManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager" >
<property name="sessionFactory"><ref bean="sessionFactory"/>
</property>
</bean> -
getCurrentSession()事务会自动关闭,所以在有所jsp页面查询数据都会关闭session。要想在jsp查询数据库需要加入:
org.springframework.orm.hibernate4.support.OpenSessionInViewFilter过滤器。 -
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 和 spring3 整合注意事项 否则java.lang.NoSuchMethodError异常
最近一直在研究S2SH开发框架,把三部分分别看完之后开始整合,发现由于 hibernate 高版本已经又完善了好多功能,导致与之前跟 spring 整合的方法有些出入。
先说一下与 hibernate3 整合的时候获取 Session 的办法吧。
以检测用户名和密码是否正确为例,数据表是users,所以使用MyEclipse反向生成其所对应的类和.hbm.xml文件分别是Users.java和Users.hbm.xml。
我是直接使用 MyEclipse 添加的两个框架,所以系统自动给生成了 applicationContext.xml 配置文件,关于 SessionFactory 的部分如下:
1 <bean id="sessionFactory" 2 class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> 3 <property name="dataSource"> 4 <ref bean="dataSource" /> 5 </property> 6 <property name="hibernateProperties"> 7 <props> 8 <prop key="hibernate.dialect"> 9 org.hibernate.dialect.MySQLDialect 10 </prop> 11 </props> 12 </property> 13 <property name="mappingResources"> 14 <list> 15 <value>com/ssh/entity/Users.hbm.xml</value> 16 </list> 17 </property> 18 </bean>
其实在这里也可以看到两个框架的整合,在sessionFactory中添加了 mappingResource 的<property>标签,实现OR映射。
如果使用hibernate3提供的接口,那么在DAO中的类要继承HibernateDaoSupport类,那么就可以使用:
1 super.getHibernateTemplate().executeFind(new HibernateCallback() { 2 public Object doInHibernate(Session session)throws HibernateException, SQLException { 3 ...... 4 } 5 }
可以看到在 doInHibernate()中的参数就是session,那我们就可以直接使用了。
关键就是现在它不是出hibernate4了么,很多人就是有强迫症,总喜欢用最高级版的,那就用呗,然后就还按照之前的这个方法去做了,结果抛异常了。。。我就是强迫症患者之一。。。异常如下:
1 Exception in thread "main" java.lang.NoSuchMethodError: org.hibernate.SessionFactory.openSession()Lorg/hibernate/classic/Session; 2 at org.springframework.orm.hibernate3.SessionFactoryUtils.doGetSession(SessionFactoryUtils.java:324) 3 at org.springframework.orm.hibernate3.SessionFactoryUtils.getSession(SessionFactoryUtils.java:235) 4 at org.springframework.orm.hibernate3.HibernateTemplate.getSession(HibernateTemplate.java:457) 5 at org.springframework.orm.hibernate3.HibernateTemplate.doExecute(HibernateTemplate.java:393) 6 at org.springframework.orm.hibernate3.HibernateTemplate.executeFind(HibernateTemplate.java:343)
很奇怪的一件事就是明明使用的hibernate4,怎么异常信息都是关于hibernate3的?然后又返回去看了一下继承的HibernateDaoSupport,其实是:
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
后来上网查了一下,主要原因就是hibernate4已经提供了很好的机制,所以不需要再继承HibernateDaoSupport类了。那怎么办呢?不就是为了获取到Session么,我们直接在UserDAOImpl.java类中添加一个SessionFactory属性,再添上它的setter方法。我们使用的DAO层的实现层是要添加到applicationContext.xml中的,其实也就是spring的依赖注入,比如我的UserDAOImol.java在xml中的添加如下:
1 <bean id="userDAO" class="com.ssh.dao.UserDAOImpl"> 2 <property name="sessionFactory" ref="sessionFactory"></property> 3 </bean>
这里的sessionFactory在上面已经提到了,这是系统自动生成的。看到这里的代码就应该会想到,其实到这里我们已经获取到了sessionFactory实例。至于它的使用肯定已经很熟悉了,不多说。我的UserDAOImpl.java代码如下:
1 package com.ssh.dao; 2 3 import java.util.List; 4 5 import org.hibernate.Criteria; 6 import org.hibernate.Session; 7 import org.hibernate.SessionFactory; 8 import org.hibernate.criterion.Restrictions; 9 10 import com.ssh.entity.Users; 11 12 public class UserDAOImpl implements UserDAO { 13 private SessionFactory sessionFactory; 14 15 public void setSessionFactory(SessionFactory sessionFactory) { 16 this.sessionFactory = sessionFactory; 17 } 18 19 public List search(final Users condition) { 20 //Session session = sessionFactory.getCurrentSession(); 21 Session session = sessionFactory.openSession(); 22 Criteria criteria = session.createCriteria(Users.class); 23 if (condition != null) { 24 String username = condition.getUsername(); 25 String password = condition.getPassword(); 26 if (username != null && !username.equals("")) { 27 criteria.add(Restrictions.eq("username", username)); 28 } 29 if (password != null && !password.equals("")) { 30 criteria.add(Restrictions.eq("password", password)); 31 } 32 } 33 return criteria.list(); 34 35 } 36 37 }
一定要注意这里第20行的注释部分,不要用getCurrentSession(),不然还是有异常。直接用openSession()