我使用spring roo构建CMS webapp。
这个应用程序当然具有用户管理功能。
我的问题是,如果我使用PhpMyAdmin将用户信息插入数据库,然后转到我的应用程序来修改thoes信息,错误orcur。 (如果我按应用程序插入用户信息并按应用程序编辑,则可以正常工作)
你们中的任何人都可以向我解释它为什么会发生以及如何解决它吗?
非常感谢。
添加信息:此错误仅由用户表或另一个表(例如新闻或产品)出现,没有问题。 以下是我的一些代码。
EndUser.java(实体)
public class EndUser {
@NotNull
@Size(min = 1, max = 20)
@Column(name = "user_id", unique = true, nullable = false)
private String userId;
@Column(name = "user_name")
private String name;
@NotNull
@Size(min = 1, max = 40)
@Column(name = "password", nullable = false)
private String password;
@Column(name = "first_name")
private String firstName;
@Column(name = "last_name")
private String lastName;
@Column(name = "gender")
private Integer gender;
@Column(name = "email")
private String email;
@Column(name = "last_login")
@DateTimeFormat(style = "M-")
private Date lastLogin;
@Column(name = "created_date")
@DateTimeFormat(style = "M-")
private Date createdDate;
@Column(name = "date_of_birth")
@DateTimeFormat(style = "M-")
private Date dateOfBirth;
@NotNull
@Column(name = "role")
private String role;
@Column(name = "is_deleted")
private Boolean isDeleted;
}
EndUserController.java
@PreAuthorize("hasRole('ROLE_SUPER_ADMIN')")
@RequestMapping(method = RequestMethod.PUT, produces = "text/html")
public String update(@Valid EndUser endUser, BindingResult bindingResult, Model uiModel, HttpServletRequest httpServletRequest) {
if (bindingResult.hasErrors()) {
populateEditForm(uiModel, endUser);
return "admin/user/update";
}
endUser.setIsDeleted(false);
uiModel.asMap().clear();
endUser.merge();
return "redirect:/admin/user/" + encodeUrlPathSegment(endUser.getId().toString(), httpServletRequest);
}
如果我更新PhpMyAdmin插入的用户信息,则错误或者
2013-05-08 20:10:58,741 [http-8080-7] DEBUG org.springframework.orm.jpa.JpaTransactionManager - Found thread-bound EntityManager [org.hibernate.ejb.EntityManagerImpl@165f5a4] for JPA transaction
2013-05-08 20:10:58,741 [http-8080-7] DEBUG org.springframework.orm.jpa.JpaTransactionManager - Creating new transaction with name [vn.a2s.lib.db.model.EndUser.merge]: PROPAGATION_REQUIRED,ISOLATION_DEFAULT; ''
2013-05-08 20:10:58,741 [http-8080-7] DEBUG org.springframework.orm.jpa.JpaTransactionManager - Not exposing JPA transaction [org.hibernate.ejb.EntityManagerImpl@165f5a4] as JDBC transaction because JpaDialect [org.springframework.orm.jpa.DefaultJpaDialect@157f3a4] does not support JDBC Connection retrieval
2013-05-08 20:10:58,743 [http-8080-7] DEBUG org.springframework.beans.factory.annotation.InjectionMetadata - Processing injected method of bean 'vn.a2s.lib.db.model.EndUser': PersistenceElement for transient javax.persistence.EntityManager vn.a2s.lib.db.model.EndUser.entityManager
2013-05-08 20:10:58,743 [http-8080-7] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Returning cached instance of singleton bean 'entityManagerFactory'
2013-05-08 20:10:58,752 [http-8080-7] ERROR org.hibernate.engine.jdbc.spi.SqlExceptionHelper - Duplicate entry 'nghi' for key 'user_id'
2013-05-08 20:10:58,752 [http-8080-7] ERROR org.hibernate.engine.jdbc.spi.SqlExceptionHelper - Duplicate entry 'nghi' for key 'user_id'
2013-05-08 20:10:58,752 [http-8080-7] DEBUG org.springframework.orm.jpa.JpaTransactionManager - Initiating transaction rollback
2013-05-08 20:10:58,752 [http-8080-7] DEBUG org.springframework.orm.jpa.JpaTransactionManager - Rolling back JPA transaction on EntityManager [org.hibernate.ejb.EntityManagerImpl@165f5a4]
2013-05-08 20:10:58,753 [http-8080-7] DEBUG org.springframework.orm.jpa.JpaTransactionManager - Not closing pre-bound JPA EntityManager after transaction
2013-05-08 20:10:58,754 [http-8080-7] DEBUG org.springframework.web.servlet.mvc.method.annotation.ExceptionHandlerExceptionResolver - Resolving exception from handler [public java.lang.String vn.a2s.sparepart.web.admin.EndUserController.update(vn.a2s.lib.db.model.EndUser,org.springframework.validation.BindingResult,org.springframework.ui.Model,javax.servlet.http.HttpServletRequest)]: org.springframework.orm.jpa.JpaSystemException: org.hibernate.exception.ConstraintViolationException: Duplicate entry 'nghi' for key 'user_id'; nested exception is javax.persistence.PersistenceException: org.hibernate.exception.ConstraintViolationException: Duplicate entry 'nghi' for key 'user_id'
2013-05-08 20:10:58,754 [http-8080-7] DEBUG org.springframework.web.servlet.mvc.annotation.ResponseStatusExceptionResolver - Resolving exception from handler [public java.lang.String vn.a2s.sparepart.web.admin.EndUserController.update(vn.a2s.lib.db.model.EndUser,org.springframework.validation.BindingResult,org.springframework.ui.Model,javax.servlet.http.HttpServletRequest)]: org.springframework.orm.jpa.JpaSystemException: org.hibernate.exception.ConstraintViolationException: Duplicate entry 'nghi' for key 'user_id'; nested exception is javax.persistence.PersistenceException: org.hibernate.exception.ConstraintViolationException: Duplicate entry 'nghi' for key 'user_id'
2013-05-08 20:10:58,754 [http-8080-7] DEBUG org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver - Resolving exception from handler [public java.lang.String vn.a2s.sparepart.web.admin.EndUserController.update(vn.a2s.lib.db.model.EndUser,org.springframework.validation.BindingResult,org.springframework.ui.Model,javax.servlet.http.HttpServletRequest)]: org.springframework.orm.jpa.JpaSystemException: org.hibernate.exception.ConstraintViolationException: Duplicate entry 'nghi' for key 'user_id'; nested exception is javax.persistence.PersistenceException: org.hibernate.exception.ConstraintViolationException: Duplicate entry 'nghi' for key 'user_id'
2013-05-08 20:10:58,754 [http-8080-7] DEBUG org.springframework.web.servlet.handler.SimpleMappingExceptionResolver - Resolving exception from handler [public java.lang.String vn.a2s.sparepart.web.admin.EndUserController.update(vn.a2s.lib.db.model.EndUser,org.springframework.validation.BindingResult,org.springframework.ui.Model,javax.servlet.http.HttpServletRequest)]: org.springframework.orm.jpa.JpaSystemException: org.hibernate.exception.ConstraintViolationException: Duplicate entry 'nghi' for key 'user_id'; nested exception is javax.persistence.PersistenceException: org.hibernate.exception.ConstraintViolationException: Duplicate entry 'nghi' for key 'user_id'
2013-05-08 20:10:58,754 [http-8080-7] DEBUG org.springframework.web.servlet.handler.SimpleMappingExceptionResolver - Resolving to view 'dataAccessFailure' for exception of type [org.springframework.orm.jpa.JpaSystemException], based on exception mapping [.DataAccessException]
2013-05-08 20:10:58,754 [http-8080-7] DEBUG org.springframework.web.servlet.handler.SimpleMappingExceptionResolver - Exposing Exception as model attribute 'exception'
2013-05-08 20:10:58,754 [http-8080-7] DEBUG org.springframework.web.servlet.DispatcherServlet - Handler execution resulted in exception - forwarding to resolved error view: ModelAndView: reference to view with name 'dataAccessFailure'; model is {exception=org.springframework.orm.jpa.JpaSystemException: org.hibernate.exception.ConstraintViolationException: Duplicate entry 'nghi' for key 'user_id'; nested exception is javax.persistence.PersistenceException: org.hibernate.exception.ConstraintViolationException: Duplicate entry 'nghi' for key 'user_id'}
org.springframework.orm.jpa.JpaSystemException: org.hibernate.exception.ConstraintViolationException: Duplicate entry 'nghi' for key 'user_id'; nested exception is javax.persistence.PersistenceException: org.hibernate.exception.ConstraintViolationException: Duplicate entry 'nghi' for key 'user_id'
at org.springframework.orm.jpa.EntityManagerFactoryUtils.convertJpaAccessExceptionIfPossible(EntityManagerFactoryUtils.java:318)
at org.springframework.orm.jpa.aspectj.JpaExceptionTranslatorAspect.ajc$afterThrowing$org_springframework_orm_jpa_aspectj_JpaExceptionTranslatorAspect$1$18a1ac9(JpaExceptionTranslatorAspect.aj:15)
at vn.a2s.lib.db.model.EndUser_Roo_Jpa_ActiveRecord.ajc$interMethod$vn_a2s_lib_db_model_EndUser_Roo_Jpa_ActiveRecord$vn_a2s_lib_db_model_EndUser$merge(EndUser_Roo_Jpa_ActiveRecord.aj:72)
at vn.a2s.lib.db.model.EndUser.merge(EndUser.java:1)
at vn.a2s.lib.db.model.EndUser_Roo_Jpa_ActiveRecord.ajc$interMethodDispatch1$vn_a2s_lib_db_model_EndUser_Roo_Jpa_ActiveRecord$vn_a2s_lib_db_model_EndUser$merge(EndUser_Roo_Jpa_ActiveRecord.aj)
at vn.a2s.sparepart.web.admin.EndUserController.update(EndUserController.java:87)
at vn.a2s.sparepart.web.admin.EndUserController$$FastClassByCGLIB$$ed0ea343.invoke(<generated>)
at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204)
at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:698)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150)
at org.springframework.security.access.intercept.aopalliance.MethodSecurityInterceptor.invoke(MethodSecurityInterceptor.java:61)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:631)
at vn.a2s.sparepart.web.admin.EndUserController$$EnhancerByCGLIB$$48aaf78c.update(<generated>)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:219)
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:132)
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:746)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:687)
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:80)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:925)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:856)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:915)
at org.springframework.web.servlet.FrameworkServlet.doPut(FrameworkServlet.java:833)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:640)
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:796)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter.doFilterInternal(OpenEntityManagerInViewFilter.java:180)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:311)
at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:116)
at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java:83)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:323)
at org.springframework.security.web.access.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:113)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:323)
at org.springframework.security.web.session.SessionManagementFilter.doFilter(SessionManagementFilter.java:101)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:323)
at org.springframework.security.web.authentication.AnonymousAuthenticationFilter.doFilter(AnonymousAuthenticationFilter.java:113)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:323)
at org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter.doFilter(SecurityContextHolderAwareRequestFilter.java:54)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:323)
at org.springframework.security.web.savedrequest.RequestCacheAwareFilter.doFilter(RequestCacheAwareFilter.java:45)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:323)
at org.springframework.security.web.authentication.www.BasicAuthenticationFilter.doFilter(BasicAuthenticationFilter.java:150)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:323)
at org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.doFilter(AbstractAuthenticationProcessingFilter.java:182)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:323)
at org.springframework.security.web.authentication.logout.LogoutFilter.doFilter(LogoutFilter.java:105)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:323)
at org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:87)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:323)
at org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:173)
at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:346)
at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:259)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:74)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:852)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:588)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
at java.lang.Thread.run(Unknown Source)
答案 0 :(得分:1)
开箱即用的Hibernate是以不支持直接修改表的方式设置的。这是因为Hibernate在读/写之间缓存实体。如果你一遍又一遍地请求相同的对象,Hibernate,如果它正确地完成它的工作,应该是读取对象一次,对缓存执行所有更新,并且只在缓存过期时写入对象。
如果修改Hibernate后面的数据库,则缓存中的项目与数据库中的项目不匹配。有一种方法可以在Hibernate中关闭它,但它有性能影响。不幸的是,自从我必须在Hibernate中执行此操作已经有一段时间了,所以你实际上如何做到这一点我不是100%肯定。你可能不得不花一些时间在Google上搜索它。