hibernate4 与 spring3有点冲突
http://blog.csdn.net/gyflyx/article/details/7632645
整合NoSuchMethodError错误 and 升级Spring3.1RC2 和Hibernate4.0.0CR7遇到的一些问题及解决
2012-02-20 10:33:28| 分类: 编程_SSH |字号 订阅
我使用的是hibernate4和spring3,然后报的错误是
java.lang.NoSuchMethodError: org.hibernate.SessionFactory.openSession()Lorg/hibernate/classic/Session;
at org.springframework.orm.hibernate3.SessionFactoryUtils.doGetSession(SessionFactoryUtils.java:324)
at org.springframework.orm.hibernate3.SessionFactoryUtils.getSession(SessionFactoryUtils.java:235)
at org.springframework.orm.hibernate3.HibernateTemplate.getSession(HibernateTemplate.java:457)
at org.springframework.orm.hibernate3.HibernateTemplate.doExecute(HibernateTemplate.java:393)
at org.springframework.orm.hibernate3.HibernateTemplate.executeWithNativeSession(HibernateTemplate.java:374)
at org.springframework.orm.hibernate3.HibernateTemplate.get(HibernateTemplate.java:512)
at org.springframework.orm.hibernate3.HibernateTemplate.get(HibernateTemplate.java:506)
at general.dao.impl.UserDaoHibernate.get(UserDaoHibernate.java:35)
at general.dao.impl.UserDaoHibernate.delete(UserDaoHibernate.java:17)
at hibernate.UserDaoHibernateTest.testDeleteUser(UserDaoHibernateTest.java:41)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:45)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:68)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
at org.junit.runners.ParentRunner.run(ParentRunner.java:300)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:46)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
竟然是找不到方法,而我肯定导入了hibernate的jar包了,在网上搜索相关的文章,有人说这是在整合ssh时常出现的错误,主要指版本间的不兼容,我想这两个框架我都使用的是最新版本啊,怎么会不兼容呢。然后去spring的jar包里面看了一下才知道系统默认的使用的是.hibernate3.SessionFactoryUtils.doGetSessio,而我使用的是hibernate4,那么原因肯定就在这里了,但是为什么在相同路径下就有.hibernate4.SessionFactoryUtils.doGetSessio这个为什么不调用呢?
上网查了一下,主要原因是spring认为hibernate4的代码写的足够好了,不用自己再写一个支持类了。所以就建议使用hibernate4的hibernateDAO了。下面一篇文章不错。学习之。
升级Spring3.1RC2 和Hibernate4.0.0CR7遇到的一些问题及解决
Spring3.1RC2支持
1. Quartz2
2. Hibernate4,
3. New HandlerMethod-based Support Classes For Annotated Controller Processing
4. Consumes and Produces @RequestMapping Conditions
5. Working With URI Template Variables In Controller Methods
6. Validation For @RequestBody Method Arguments //and so on....
7. Spring MVC 3.1 的annotation可以参看下http://starscream.iteye.com/blog/1098880
Hibernate 4可以查看http://community.jboss.org/wiki/HibernateCoreMigrationGuide40
下面主要说一下我在升级过程中遇到的一些问题及解决办法。
l Maven的repository始终无法升级到SpringRC2,可能服务器有问题吧,目前暂时是从官方下载的整个SpringRC2的zip包。版本号是:3.1.0.RC2
l Hibernate可以从repository中升级到4.0.0.CR7,新增的依赖包有jandex-1.0.3.Final.jar,jboss-logging-3.1.0.CR2.jar,jboss-transaction-api_1.1_spec-1.0.0.Final.jar。
l Quartz升级到2.1.1,Ehcache-core升级到2.5.0
l Spring3.1取消了HibernateTemplate,因为Hibernate4的事务管理已经很好了,不用Spring再扩展了。所以以前的Dao需要改写,直接调用Hibernate 的Session进行持久化。
l Spring的配置:
sessionFactory从org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean换成org.springframework.orm.hibernate4.LocalSessionFactoryBean
l Spring的配置:
<prop key="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</prop>改为
<prop key="hibernate.cache.provider_class">net.sf.ehcache.hibernate.EhCacheProvider</prop>
EhCacheRegionFactory使用配置:
<prop key="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory</prop>
l 使用Hibernate所有的openSession()改为getCurrentSession()
l Spring 的配置:Hibernate transactionManager从3改为4,如下:
<bean id="txManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
l Spring @ResponseBody输出是乱码的问题:原来使用的是:
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
改为:
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
<property name = "messageConverters">
<list>
<bean class = "org.springframework.http.converter.StringHttpMessageConverter">
<property name = "supportedMediaTypes">
<list>
<value>text/plain;charset=UTF-8</value>
</list>
</property>
</bean>
<bean class = "org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
<property name = "supportedMediaTypes">
<list>
<value>text/plain;charset=UTF-8</value>
<value>application/json;charset=UTF-8</value>
</list>
</property>
</bean>
</list>
</property>
这样比每个Controller都加上@RequestMapping(value = "/showLeft", method = RequestMethod.GET)
produces = "text/plain; charset=utf-8"方便的多。
l Blob,以前配置:
@TypeDefs({@TypeDef(name="clob",typeClass=ClobStringType.class),@TypeDef(name="blob",typeClass=BlobByteArrayType.class)})
@Lob
@Type(type="blob")
public byte[] getPic() {
return pic;
}
现在改为:
@Lob
public byte[] getPic() {
return pic;
}
简单很多。