开发者社区> 问答> 正文

java.lang.NoSuchMethodError 400 请求报错 

java.lang.NoSuchMethodError: org.hibernate.SessionFactory.getCurrentSession()Lorg/hibernate/classic/Session;

hibernate4.2   +spring3.2.2 +com.googlecode.genericdao 整合报错了。。

java.lang.reflect.InvocationTargetException 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 com.opensymphony.xwork2.DefaultActionInvocation.invokeAction(DefaultActionInvocation.java:450) at com.opensymphony.xwork2.DefaultActionInvocation.invokeActionOnly(DefaultActionInvocation.java:289) at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:252) at org.apache.struts2.interceptor.debugging.DebuggingInterceptor.intercept(DebuggingInterceptor.java:256) at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246) at com.opensymphony.xwork2.interceptor.DefaultWorkflowInterceptor.doIntercept(DefaultWorkflowInterceptor.java:176) at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98) at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246) at com.opensymphony.xwork2.validator.ValidationInterceptor.doIntercept(ValidationInterceptor.java:265) at org.apache.struts2.interceptor.validation.AnnotationValidationInterceptor.doIntercept(AnnotationValidationInterceptor.java:68) at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98) at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246) at com.opensymphony.xwork2.interceptor.ConversionErrorInterceptor.intercept(ConversionErrorInterceptor.java:138) at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246) at com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:249) at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98) at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246) at com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:249) at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98) at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246) at com.opensymphony.xwork2.interceptor.StaticParametersInterceptor.intercept(StaticParametersInterceptor.java:191) at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246) at org.apache.struts2.interceptor.MultiselectInterceptor.intercept(MultiselectInterceptor.java:73) at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246) at org.apache.struts2.interceptor.CheckboxInterceptor.intercept(CheckboxInterceptor.java:91) at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246) at org.apache.struts2.interceptor.FileUploadInterceptor.intercept(FileUploadInterceptor.java:252) at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246) at com.opensymphony.xwork2.interceptor.ModelDrivenInterceptor.intercept(ModelDrivenInterceptor.java:100) at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246) at com.opensymphony.xwork2.interceptor.ScopedModelDrivenInterceptor.intercept(ScopedModelDrivenInterceptor.java:141) at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246) at com.opensymphony.xwork2.interceptor.ChainingInterceptor.intercept(ChainingInterceptor.java:145) at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246) at com.opensymphony.xwork2.interceptor.PrepareInterceptor.doIntercept(PrepareInterceptor.java:171) at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98) at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246) at com.opensymphony.xwork2.interceptor.I18nInterceptor.intercept(I18nInterceptor.java:176) at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246) at org.apache.struts2.interceptor.ServletConfigInterceptor.intercept(ServletConfigInterceptor.java:164) at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246) at com.opensymphony.xwork2.interceptor.AliasInterceptor.intercept(AliasInterceptor.java:193) at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246) at com.opensymphony.xwork2.interceptor.ExceptionMappingInterceptor.intercept(ExceptionMappingInterceptor.java:187) at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246) at org.apache.struts2.impl.StrutsActionProxy.execute(StrutsActionProxy.java:54) at org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:546) at org.apache.struts2.dispatcher.ng.ExecuteOperations.executeAction(ExecuteOperations.java:77) at org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.doFilter(StrutsPrepareAndExecuteFilter.java:91) at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1212) at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88) at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1212) at org.springframework.orm.hibernate4.support.OpenSessionInViewFilter.doFilterInternal(OpenSessionInViewFilter.java:152) at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1212) at org.mortbay.jetty.servlet.ServletHandler.handle(ServletHandler.java:399) at org.mortbay.jetty.security.SecurityHandler.handle(SecurityHandler.java:216) at org.mortbay.jetty.servlet.SessionHandler.handle(SessionHandler.java:182) at org.mortbay.jetty.handler.ContextHandler.handle(ContextHandler.java:766) at org.mortbay.jetty.webapp.WebAppContext.handle(WebAppContext.java:450) at org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:152) at org.mortbay.jetty.Server.handle(Server.java:326) at org.mortbay.jetty.HttpConnection.handleRequest(HttpConnection.java:542) at org.mortbay.jetty.HttpConnection$RequestHandler.headerComplete(HttpConnection.java:928) at org.mortbay.jetty.HttpParser.parseNext(HttpParser.java:549) at org.mortbay.jetty.HttpParser.parseAvailable(HttpParser.java:212) at org.mortbay.jetty.HttpConnection.handle(HttpConnection.java:404) at org.mortbay.io.nio.SelectChannelEndPoint.run(SelectChannelEndPoint.java:410) at org.mortbay.thread.QueuedThreadPool$PoolThread.run(QueuedThreadPool.java:582) Caused by: java.lang.NoSuchMethodError: org.hibernate.SessionFactory.getCurrentSession()Lorg/hibernate/classic/Session; at com.googlecode.genericdao.dao.hibernate.HibernateBaseDAO.getSession(HibernateBaseDAO.java:68) at com.googlecode.genericdao.dao.hibernate.HibernateBaseDAO._searchAndCount(HibernateBaseDAO.java:571) at com.googlecode.genericdao.dao.hibernate.GenericDAOImpl.searchAndCount(GenericDAOImpl.java:121) at com.blog.lyf.modules.blog.service.impl.UserServiceImpl.searchAndCount(UserServiceImpl.java:98) at com.blog.lyf.modules.blog.action.UserAction.execute(UserAction.java:204) ... 74 more

展开
收起
kun坤 2020-05-28 16:04:02 453 0
1 条回答
写回答
取消 提交回答
  • 应该是还在使用HibernateTemplate的问题,Hibernate4中Spring不再提供Template了而是改用Hibernate的原生Session,建议自行Google一下Hibernate4 和 Spring

    ######回复 @姚君 : Heibernate自带的SessionFactory的getCurrentSession是可以用的,不过hibernate4的Session已经不在classic包了,所以Spring内部的那些就没法了######可以用openSession 但是 不能用getCurrentSession######我知道你的问题在哪里!首先说明下, getCurrentSession()是能用的,但是有几点必须要注意,这是Hibernate4和Hibernate3不一样的地方

    1.不能在使用HibernateTemplate,Spring已经不建议使用了,应该将SessionFactory直接注入到DAO,而不再是HibernateTemplate

    2.不能使用openSession,openSession获取的session是不受Spring切面事务管理的,必须使用 getCurrentSession ,而且必须开启事务, getCurrentSession 才能正确获取到,因为在Spring3中session是被绑定在SpringSessionContext中的,所以

    getCurrentSession 是从这个上下文的线程局部变量去获取session,而且只有开启事务了,session才会绑定到这个上下文中

    ######

    <!-- 会话工厂 --> <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> <property name="dataSource" ref="dataSource-c3p0"/> <property name="hibernateProperties"> <props> <prop key="hiberante.dialect">org.hibernate.dialect.MySQLDialect</prop> <prop key="hibernate.show_sql">false</prop> <prop key="hibernate.format_sql">false</prop> <prop key="hibernate.hbm2ddl.auto">update</prop> <!-- <prop key="hibernate.current_session_context_class">org.springframework.orm.hibernate4.SpringSessionContext</prop> --> </props> </property> <property name="packagesToScan"> <!-- 多个包用逗号分割 --> <value>com.ymxx.jweb.persistence</value> </property> </bean> <!-- 事务管理器--> <bean id="transactionManager-hibernate" abstract="false" lazy-init="default" autowire="default" class="org.springframework.orm.hibernate4.HibernateTransactionManager"> <property name="sessionFactory"> <ref bean="sessionFactory" /> </property> </bean> <tx:annotation-driven/>
    ###### <prop key="hibernate.current_session_context_class">org.springframework.orm.hibernate4.SpringSessionContext</prop>
    注意这一行,配置文件里不要写,开启事务后,Spring会自动帮你绑定,这就告诉你,session是从这里面取的,但是要开启事务才能获取到 ######我比较喜欢使用标注型事务,而不是xml风格的事务:

    @Service("accountService")
    @Transactional("transactionManager-hibernate")
    public class AccountService {
    这样就在service上开启事务了,就能通过currentSessin获取链接了,这个事务会应用的该service中所有的public方法上,默认事务传播属性为required,这些事务属性你可以自定义

    ######这个东西要看Spring3的文档,或者源代码才能知道,我一开始也被折腾了半条命,其实Spring3更简洁更强大,只是我们走得弯路多而已######我找到原因了,是因为com.googlecode.genericdao 这个框架的原因,sessionFactory不能注入进去,不知道为什么。######

    引用来自“hoodlake”的答案

    <!-- 会话工厂 -->
    	<bean id="sessionFactory"
    			class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
    		<property name="dataSource" ref="dataSource-c3p0"/>
    		<property name="hibernateProperties">
    			<props>
    				<prop key="hiberante.dialect">org.hibernate.dialect.MySQLDialect</prop>
    				<prop key="hibernate.show_sql">false</prop>
    				<prop key="hibernate.format_sql">false</prop>
    				<prop key="hibernate.hbm2ddl.auto">update</prop>
    				<!-- 
    					<prop key="hibernate.current_session_context_class">org.springframework.orm.hibernate4.SpringSessionContext</prop>
    				-->
    			</props>
    		</property>
    		<property name="packagesToScan">
    			<!-- 多个包用逗号分割 -->
    			<value>com.ymxx.jweb.persistence</value>
    		</property>
    	</bean>
    	<!-- 事务管理器-->
    	<bean id="transactionManager-hibernate" abstract="false" lazy-init="default" autowire="default"
    			class="org.springframework.orm.hibernate4.HibernateTransactionManager">
    		<property name="sessionFactory">
    			<ref bean="sessionFactory" />
    		</property>
    	</bean>
    	<tx:annotation-driven/>
    我也是这么配置的,只是我com.googlecode.genericdao不能注入sessionFctroy...自己写dao就可以用 ######com.googlecode.genericdao,这个如果不是你自己写的,那里面有SessionFactory这个成员变量吗?或者你可以继承下这个DAO,然后在子类里写个SessionFactory,然后注入到子类 ######写了子类,但是注入不了,不知道为什么,换成hibernate3就行######也碰到此问题,应该怎么办呢 =============================== https://developer.aliyun.com/ask/290761 -*-*- 学习了,######5.0以后这招不好使了吧 大忽悠
    2020-05-29 11:14:58
    赞同 展开评论 打赏
问答标签:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
Spring Cloud Alibaba - 重新定义 Java Cloud-Native 立即下载
The Reactive Cloud Native Arch 立即下载
JAVA开发手册1.5.0 立即下载