一、问题描述
通过jpa映射id的生成方式通过@GeneratedValue(strategy=GenerationType.AUTO)注释为数据库本地自增长方式,如mysql自动递增方式,但报出“org.hibernate.HibernateException: The database returned no natively generated identity value”异常,具体如下所示
[YOMS] 18:31:26,911 ERROR com.woopa.sf.web.action.UserMgtAction,225 - The database returned no natively generated identity value@b@org.hibernate.HibernateException: The database returned no natively generated identity value@b@ at org.hibernate.id.IdentifierGeneratorHelper.getGeneratedIdentity(IdentifierGeneratorHelper.java:90)@b@ at org.hibernate.id.IdentityGenerator$GetGeneratedKeysDelegate.executeAndExtract(IdentityGenerator.java:100)@b@ at org.hibernate.id.insert.AbstractReturningDelegate.performInsert(AbstractReturningDelegate.java:58)@b@ at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2975)@b@ at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:3487)@b@ at org.hibernate.action.internal.EntityIdentityInsertAction.execute(EntityIdentityInsertAction.java:81)@b@ at org.hibernate.engine.spi.ActionQueue.execute(ActionQueue.java:377)@b@ at org.hibernate.engine.spi.ActionQueue.addResolvedEntityInsertAction(ActionQueue.java:214)@b@ at org.hibernate.engine.spi.ActionQueue.addInsertAction(ActionQueue.java:194)@b@ at org.hibernate.engine.spi.ActionQueue.addAction(ActionQueue.java:178)@b@ at org.hibernate.event.internal.AbstractSaveEventListener.addInsertAction(AbstractSaveEventListener.java:321)@b@ at org.hibernate.event.internal.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:286)@b@ at org.hibernate.event.internal.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:192)@b@ at org.hibernate.event.internal.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:125)@b@ at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:206)@b@ at org.hibernate.event.internal.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:55)@b@ at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:191)@b@ at org.hibernate.event.internal.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:49)@b@ at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:90)@b@ at org.hibernate.internal.SessionImpl.fireSave(SessionImpl.java:764)@b@ at org.hibernate.internal.SessionImpl.save(SessionImpl.java:756)@b@ at org.hibernate.internal.SessionImpl.save(SessionImpl.java:752)@b@ at com.woopa.common.persist.hibernate4.BaseHibernateDao.save(BaseHibernateDao.java:593)@b@ at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)@b@ at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)@b@ at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)@b@ at java.lang.reflect.Method.invoke(Method.java:597)@b@ at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:317)@b@ at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:183)@b@ at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150)@b@ at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:96)@b@ at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:260)@b@ at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:94)@b@ at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)@b@ at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:91)@b@ at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)@b@ at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:204)@b@ at sun.proxy.$Proxy30.save(Unknown Source)@b@ at com.woopa.sf.service.ActionLogService.save(ActionLogService.java:36)@b@ at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)@b@ at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)@b@ at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)@b@ at java.lang.reflect.Method.invoke(Method.java:597)@b@ at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:317)@b@ at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:183)@b@ at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150)@b@ at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:96)@b@ at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:260)@b@ at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:94)@b@ at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)@b@ at org.springframework.aop.framework.adapter.MethodBeforeAdviceInterceptor.invoke(MethodBeforeAdviceInterceptor.java:51)@b@ at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)@b@ at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:91)@b@ at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)@b@ at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:204)@b@ at sun.proxy.$Proxy36.save(Unknown Source)@b@ at com.woopa.sf.log.ActionLogAppender.append(ActionLogAppender.java:30)@b@ at com.woopa.sf.log.LogAppenderRegistry.onApplicationEvent(LogAppenderRegistry.java:39)@b@ at com.woopa.sf.log.LogAppenderRegistry.onApplicationEvent(LogAppenderRegistry.java:1)@b@ at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:96)@b@ at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:334)@b@ at com.woopa.sf.SecurityManager.log(SecurityManager.java:70)@b@ at com.woopa.sf.log.LogHelper.actionLog(LogHelper.java:39)@b@ at com.woopa.sf.log.LogHelper.actionLog(LogHelper.java:26)@b@ at com.woopa.sf.service.UserService.afterAdd(UserService.java:270)@b@ at com.woopa.sf.service.UserService.create(UserService.java:75)@b@ at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)@b@ at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)@b@ at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)@b@ at java.lang.reflect.Method.invoke(Method.java:597)@b@ at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:317)@b@ at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:183)@b@ at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150)@b@ at org.springframework.security.intercept.method.aopalliance.MethodSecurityInterceptor.invoke(MethodSecurityInterceptor.java:66)@b@ at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)@b@ at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:96)@b@ at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:260)@b@ at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:94)@b@ at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)@b@ at org.springframework.aop.framework.adapter.MethodBeforeAdviceInterceptor.invoke(MethodBeforeAdviceInterceptor.java:51)@b@ at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)@b@ at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:91)@b@ at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)@b@ at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:204)@b@ at sun.proxy.$Proxy49.create(Unknown Source)@b@ at com.woopa.sf.web.action.UserMgtAction.add(UserMgtAction.java:221)@b@ at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)@b@ at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)@b@ at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)@b@ at java.lang.reflect.Method.invoke(Method.java:597)@b@ at com.opensymphony.xwork2.DefaultActionInvocation.invokeAction(DefaultActionInvocation.java:450)@b@ at com.opensymphony.xwork2.DefaultActionInvocation.invokeActionOnly(DefaultActionInvocation.java:289)@b@ at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:252)@b@ at org.apache.struts2.interceptor.debugging.DebuggingInterceptor.intercept(DebuggingInterceptor.java:256)@b@ at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246)@b@ at com.opensymphony.xwork2.interceptor.DefaultWorkflowInterceptor.doIntercept(DefaultWorkflowInterceptor.java:167)@b@ at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)@b@ at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246)@b@ at com.opensymphony.xwork2.validator.ValidationInterceptor.doIntercept(ValidationInterceptor.java:265)@b@ at org.apache.struts2.interceptor.validation.AnnotationValidationInterceptor.doIntercept(AnnotationValidationInterceptor.java:68)@b@ at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)@b@ at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246)@b@ at com.opensymphony.xwork2.interceptor.ConversionErrorInterceptor.intercept(ConversionErrorInterceptor.java:138)@b@ at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246)@b@ at com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:239)@b@ at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)@b@ at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246)@b@ at com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:239)@b@ at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)@b@ at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246)@b@ at com.opensymphony.xwork2.interceptor.StaticParametersInterceptor.intercept(StaticParametersInterceptor.java:191)@b@ at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246)@b@ at org.apache.struts2.interceptor.MultiselectInterceptor.intercept(MultiselectInterceptor.java:73)@b@ at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246)@b@ at org.apache.struts2.interceptor.CheckboxInterceptor.intercept(CheckboxInterceptor.java:91)@b@ at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246)@b@ at org.apache.struts2.interceptor.FileUploadInterceptor.intercept(FileUploadInterceptor.java:252)@b@ at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246)@b@ at com.opensymphony.xwork2.interceptor.ModelDrivenInterceptor.intercept(ModelDrivenInterceptor.java:100)@b@ at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246)@b@ at com.opensymphony.xwork2.interceptor.ScopedModelDrivenInterceptor.intercept(ScopedModelDrivenInterceptor.java:141)@b@ at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246)@b@ at com.opensymphony.xwork2.interceptor.ChainingInterceptor.intercept(ChainingInterceptor.java:145)@b@ at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246)@b@ at com.opensymphony.xwork2.interceptor.PrepareInterceptor.doIntercept(PrepareInterceptor.java:171)@b@ at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)@b@ at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246)@b@ at com.opensymphony.xwork2.interceptor.I18nInterceptor.intercept(I18nInterceptor.java:161)@b@ at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246)@b@ at org.apache.struts2.interceptor.ServletConfigInterceptor.intercept(ServletConfigInterceptor.java:164)@b@ at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246)@b@ at com.opensymphony.xwork2.interceptor.AliasInterceptor.intercept(AliasInterceptor.java:193)@b@ at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246)@b@ at com.opensymphony.xwork2.interceptor.ExceptionMappingInterceptor.intercept(ExceptionMappingInterceptor.java:189)@b@ at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246)@b@ at org.apache.struts2.impl.StrutsActionProxy.execute(StrutsActionProxy.java:54)@b@ at org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:563)@b@ at org.apache.struts2.dispatcher.ng.ExecuteOperations.executeAction(ExecuteOperations.java:77)@b@ at org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.doFilter(StrutsPrepareAndExecuteFilter.java:99)@b@ at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)@b@ at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)@b@ at com.woopa.sf.sec.SecurityFilter.doFilter(SecurityFilter.java:48)@b@ at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)@b@ at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)@b@ at org.springframework.security.util.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:378)@b@ at org.springframework.security.intercept.web.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:99)@b@ at org.springframework.security.intercept.web.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java:83)@b@ at org.springframework.security.util.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:390)@b@ at org.springframework.security.intercept.web.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:109)@b@ at org.springframework.security.intercept.web.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java:83)@b@ at org.springframework.security.util.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:390)@b@ at org.springframework.security.ui.SessionFixationProtectionFilter.doFilterHttp(SessionFixationProtectionFilter.java:67)@b@ at org.springframework.security.ui.SpringSecurityFilter.doFilter(SpringSecurityFilter.java:53)@b@ at org.springframework.security.util.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:390)@b@ at org.springframework.security.ui.ExceptionTranslationFilter.doFilterHttp(ExceptionTranslationFilter.java:101)@b@ at org.springframework.security.ui.SpringSecurityFilter.doFilter(SpringSecurityFilter.java:53)@b@ at org.springframework.security.util.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:390)@b@ at org.springframework.security.providers.anonymous.AnonymousProcessingFilter.doFilterHttp(AnonymousProcessingFilter.java:105)@b@ at org.springframework.security.ui.SpringSecurityFilter.doFilter(SpringSecurityFilter.java:53)@b@ at org.springframework.security.util.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:390)@b@ at org.springframework.security.ui.rememberme.RememberMeProcessingFilter.doFilterHttp(RememberMeProcessingFilter.java:116)@b@ at org.springframework.security.ui.SpringSecurityFilter.doFilter(SpringSecurityFilter.java:53)@b@ at org.springframework.security.util.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:390)@b@ at org.springframework.security.wrapper.SecurityContextHolderAwareRequestFilter.doFilterHttp(SecurityContextHolderAwareRequestFilter.java:91)@b@ at org.springframework.security.ui.SpringSecurityFilter.doFilter(SpringSecurityFilter.java:53)@b@ at org.springframework.security.util.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:390)@b@ at org.springframework.security.ui.AbstractProcessingFilter.doFilterHttp(AbstractProcessingFilter.java:278)@b@ at org.springframework.security.ui.SpringSecurityFilter.doFilter(SpringSecurityFilter.java:53)@b@ at org.springframework.security.util.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:390)@b@ at org.springframework.security.ui.logout.LogoutFilter.doFilterHttp(LogoutFilter.java:89)@b@ at org.springframework.security.ui.SpringSecurityFilter.doFilter(SpringSecurityFilter.java:53)@b@ at org.springframework.security.util.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:390)@b@ at org.springframework.security.context.HttpSessionContextIntegrationFilter.doFilterHttp(HttpSessionContextIntegrationFilter.java:235)@b@ at org.springframework.security.ui.SpringSecurityFilter.doFilter(SpringSecurityFilter.java:53)@b@ at org.springframework.security.util.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:390)@b@ at org.springframework.security.concurrent.ConcurrentSessionFilter.doFilterHttp(ConcurrentSessionFilter.java:99)@b@ at org.springframework.security.ui.SpringSecurityFilter.doFilter(SpringSecurityFilter.java:53)@b@ at org.springframework.security.util.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:390)@b@ at org.springframework.security.util.FilterChainProxy.doFilter(FilterChainProxy.java:175)@b@ at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:343)@b@ at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:260)@b@ at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)@b@ at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)@b@ at org.ajaxanywhere.AAFilter.doFilter(AAFilter.java:46)@b@ at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)@b@ at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)@b@ at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88)@b@ at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)@b@ at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)@b@ at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)@b@ at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)@b@ at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)@b@ at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)@b@ at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)@b@ at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)@b@ at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)@b@ at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:857)@b@ at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:588)@b@ at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)@b@ at java.lang.Thread.run(Thread.java:662)
二、解决方式
如下图所示,mysql数据库表选择ID设置为自动递增方式,问题解决了