尝试使用@CreatedBy更新任何实体时的java.lang.StackOverflowError

时间:2016-01-12 20:39:46

标签: spring hibernate spring-mvc spring-data spring-data-jpa

this旧春季论坛帖子中描述的问题很好。

现在我遇到了同样的问题。当我尝试使用来自网络请求的2016-01-12 22:14:02.941 ERROR 8720 --- [nio-8080-exec-1] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is java.lang.RuntimeException: java.lang.StackOverflowError] with root cause java.lang.StackOverflowError: null at java.util.Collections$SynchronizedMap.get(Unknown Source) ~[na:1.8.0_66] at org.springframework.data.util.ClassTypeInformation.from(ClassTypeInformation.java:74) ~[spring-data-commons-1.11.0.RELEASE.jar:na] at org.springframework.data.mapping.context.AbstractMappingContext.hasPersistentEntityFor(AbstractMappingContext.java:148) ~[spring-data-commons-1.11.0.RELEASE.jar:na] at org.springframework.data.mapping.context.PersistentEntities.getPersistentEntity(PersistentEntities.java:62) ~[spring-data-commons-1.11.0.RELEASE.jar:na] at org.springframework.data.auditing.MappingAuditableBeanWrapperFactory.getBeanWrapperFor(MappingAuditableBeanWrapperFactory.java:76) ~[spring-data-commons-1.11.0.RELEASE.jar:na] at org.springframework.data.auditing.AuditingHandler.touch(AuditingHandler.java:139) ~[spring-data-commons-1.11.0.RELEASE.jar:na] at org.springframework.data.auditing.AuditingHandler.markModified(AuditingHandler.java:134) ~[spring-data-commons-1.11.0.RELEASE.jar:na] at org.springframework.data.jpa.domain.support.AuditingEntityListener.touchForUpdate(AuditingEntityListener.java:96) ~[spring-data-jpa-1.9.0.RELEASE.jar:na] at sun.reflect.GeneratedMethodAccessor147.invoke(Unknown Source) ~[na:na] at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[na:1.8.0_66] at java.lang.reflect.Method.invoke(Unknown Source) ~[na:1.8.0_66] at org.hibernate.jpa.event.internal.jpa.ListenerCallback.performCallback(ListenerCallback.java:32) ~[hibernate-entitymanager-5.0.3.Final.jar:5.0.3.Final] at org.hibernate.jpa.event.internal.jpa.CallbackRegistryImpl.callback(CallbackRegistryImpl.java:95) ~[hibernate-entitymanager-5.0.3.Final.jar:5.0.3.Final] at org.hibernate.jpa.event.internal.jpa.CallbackRegistryImpl.preUpdate(CallbackRegistryImpl.java:59) ~[hibernate-entitymanager-5.0.3.Final.jar:5.0.3.Final] at org.hibernate.jpa.event.internal.core.JpaFlushEntityEventListener.invokeInterceptor(JpaFlushEntityEventListener.java:51) ~[hibernate-entitymanager-5.0.3.Final.jar:5.0.3.Final] at org.hibernate.event.internal.DefaultFlushEntityEventListener.handleInterception(DefaultFlushEntityEventListener.java:325) ~[hibernate-core-5.0.3.Final.jar:5.0.3.Final] at org.hibernate.event.internal.DefaultFlushEntityEventListener.scheduleUpdate(DefaultFlushEntityEventListener.java:276) ~[hibernate-core-5.0.3.Final.jar:5.0.3.Final] at org.hibernate.event.internal.DefaultFlushEntityEventListener.onFlushEntity(DefaultFlushEntityEventListener.java:143) ~[hibernate-core-5.0.3.Final.jar:5.0.3.Final] at org.hibernate.event.internal.AbstractFlushingEventListener.flushEntities(AbstractFlushingEventListener.java:216) ~[hibernate-core-5.0.3.Final.jar:5.0.3.Final] at org.hibernate.event.internal.AbstractFlushingEventListener.flushEverythingToExecutions(AbstractFlushingEventListener.java:85) ~[hibernate-core-5.0.3.Final.jar:5.0.3.Final] at org.hibernate.event.internal.DefaultAutoFlushEventListener.onAutoFlush(DefaultAutoFlushEventListener.java:44) ~[hibernate-core-5.0.3.Final.jar:5.0.3.Final] at org.hibernate.internal.SessionImpl.autoFlushIfRequired(SessionImpl.java:1193) ~[hibernate-core-5.0.3.Final.jar:5.0.3.Final] at org.hibernate.internal.SessionImpl.list(SessionImpl.java:1261) ~[hibernate-core-5.0.3.Final.jar:5.0.3.Final] at org.hibernate.internal.QueryImpl.list(QueryImpl.java:87) ~[hibernate-core-5.0.3.Final.jar:5.0.3.Final] at org.hibernate.jpa.internal.QueryImpl.list(QueryImpl.java:567) ~[hibernate-entitymanager-5.0.3.Final.jar:5.0.3.Final] at org.hibernate.jpa.internal.QueryImpl.getSingleResult(QueryImpl.java:482) ~[hibernate-entitymanager-5.0.3.Final.jar:5.0.3.Final] at org.hibernate.jpa.criteria.compile.CriteriaQueryTypeQueryAdapter.getSingleResult(CriteriaQueryTypeQueryAdapter.java:54) ~[hibernate-entitymanager-5.0.3.Final.jar:5.0.3.Final] at org.springframework.data.jpa.repository.query.JpaQueryExecution$SingleEntityExecution.doExecute(JpaQueryExecution.java:206) ~[spring-data-jpa-1.9.0.RELEASE.jar:na] at org.springframework.data.jpa.repository.query.JpaQueryExecution.execute(JpaQueryExecution.java:78) ~[spring-data-jpa-1.9.0.RELEASE.jar:na] at org.springframework.data.jpa.repository.query.AbstractJpaQuery.doExecute(AbstractJpaQuery.java:100) ~[spring-data-jpa-1.9.0.RELEASE.jar:na] at org.springframework.data.jpa.repository.query.AbstractJpaQuery.execute(AbstractJpaQuery.java:91) ~[spring-data-jpa-1.9.0.RELEASE.jar:na] at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.doInvoke(RepositoryFactorySupport.java:454) ~[spring-data-commons-1.11.0.RELEASE.jar:na] at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.invoke(RepositoryFactorySupport.java:432) ~[spring-data-commons-1.11.0.RELEASE.jar:na] at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) ~[spring-aop-4.2.2.RELEASE.jar:4.2.2.RELEASE] at org.springframework.data.projection.DefaultMethodInvokingMethodInterceptor.invoke(DefaultMethodInvokingMethodInterceptor.java:61) ~[spring-data-commons-1.11.0.RELEASE.jar:na] at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) ~[spring-aop-4.2.2.RELEASE.jar:4.2.2.RELEASE] at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:99) ~[spring-tx-4.2.2.RELEASE.jar:4.2.2.RELEASE] at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:281) ~[spring-tx-4.2.2.RELEASE.jar:4.2.2.RELEASE] at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96) ~[spring-tx-4.2.2.RELEASE.jar:4.2.2.RELEASE] at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) ~[spring-aop-4.2.2.RELEASE.jar:4.2.2.RELEASE] at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:136) ~[spring-tx-4.2.2.RELEASE.jar:4.2.2.RELEASE] at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) ~[spring-aop-4.2.2.RELEASE.jar:4.2.2.RELEASE] at org.springframework.data.jpa.repository.support.CrudMethodMetadataPostProcessor$CrudMethodMetadataPopulatingMethodInterceptor.invoke(CrudMethodMetadataPostProcessor.java:101) ~[spring-data-jpa-1.9.0.RELEASE.jar:na] at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) ~[spring-aop-4.2.2.RELEASE.jar:4.2.2.RELEASE] at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92) ~[spring-aop-4.2.2.RELEASE.jar:4.2.2.RELEASE] at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) ~[spring-aop-4.2.2.RELEASE.jar:4.2.2.RELEASE] at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:207) ~[spring-aop-4.2.2.RELEASE.jar:4.2.2.RELEASE] at com.sun.proxy.$Proxy474.findByLogin(Unknown Source) ~[na:na] at point.office.CurrentUserImpl.get(CurrentUserImpl.java:42) ~[bin/:na] at sun.reflect.GeneratedMethodAccessor290.invoke(Unknown Source) ~[na:na] at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[na:1.8.0_66] at java.lang.reflect.Method.invoke(Unknown Source) ~[na:1.8.0_66] at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:302) ~[spring-aop-4.2.2.RELEASE.jar:4.2.2.RELEASE] at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:190) ~[spring-aop-4.2.2.RELEASE.jar:4.2.2.RELEASE] at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157) ~[spring-aop-4.2.2.RELEASE.jar:4.2.2.RELEASE] at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:99) ~[spring-tx-4.2.2.RELEASE.jar:4.2.2.RELEASE] at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:281) ~[spring-tx-4.2.2.RELEASE.jar:4.2.2.RELEASE] at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96) ~[spring-tx-4.2.2.RELEASE.jar:4.2.2.RELEASE] at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) ~[spring-aop-4.2.2.RELEASE.jar:4.2.2.RELEASE] at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:207) ~[spring-aop-4.2.2.RELEASE.jar:4.2.2.RELEASE] at com.sun.proxy.$Proxy480.get(Unknown Source) ~[na:na] at point.office.SpringSecurityAuditorAware.getCurrentAuditor(SpringSecurityAuditorAware.java:21) ~[bin/:na] at point.office.SpringSecurityAuditorAware.getCurrentAuditor(SpringSecurityAuditorAware.java:1) ~[bin/:na] at org.springframework.data.auditing.AuditingHandler.touchAuditor(AuditingHandler.java:166) ~[spring-data-commons-1.11.0.RELEASE.jar:na] at org.springframework.data.auditing.AuditingHandler.touch(AuditingHandler.java:145) ~[spring-data-commons-1.11.0.RELEASE.jar:na] at org.springframework.data.auditing.AuditingHandler.markModified(AuditingHandler.java:134) ~[spring-data-commons-1.11.0.RELEASE.jar:na] at org.springframework.data.jpa.domain.support.AuditingEntityListener.touchForUpdate(AuditingEntityListener.java:96) ~[spring-data-jpa-1.9.0.RELEASE.jar:na] at sun.reflect.GeneratedMethodAccessor147.invoke(Unknown Source) ~[na:na] at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[na:1.8.0_66] at java.lang.reflect.Method.invoke(Unknown Source) ~[na:1.8.0_66] at org.hibernate.jpa.event.internal.jpa.ListenerCallback.performCallback(ListenerCallback.java:32) ~[hibernate-entitymanager-5.0.3.Final.jar:5.0.3.Final] at org.hibernate.jpa.event.internal.jpa.CallbackRegistryImpl.callback(CallbackRegistryImpl.java:95) ~[hibernate-entitymanager-5.0.3.Final.jar:5.0.3.Final] at org.hibernate.jpa.event.internal.jpa.CallbackRegistryImpl.preUpdate(CallbackRegistryImpl.java:59) ~[hibernate-entitymanager-5.0.3.Final.jar:5.0.3.Final] at org.hibernate.jpa.event.internal.core.JpaFlushEntityEventListener.invokeInterceptor(JpaFlushEntityEventListener.java:51) ~[hibernate-entitymanager-5.0.3.Final.jar:5.0.3.Final] at org.hibernate.event.internal.DefaultFlushEntityEventListener.handleInterception(DefaultFlushEntityEventListener.java:325) ~[hibernate-core-5.0.3.Final.jar:5.0.3.Final] at org.hibernate.event.internal.DefaultFlushEntityEventListener.scheduleUpdate(DefaultFlushEntityEventListener.java:276) ~[hibernate-core-5.0.3.Final.jar:5.0.3.Final] at org.hibernate.event.internal.DefaultFlushEntityEventListener.onFlushEntity(DefaultFlushEntityEventListener.java:143) ~[hibernate-core-5.0.3.Final.jar:5.0.3.Final] at org.hibernate.event.internal.AbstractFlushingEventListener.flushEntities(AbstractFlushingEventListener.java:216) ~[hibernate-core-5.0.3.Final.jar:5.0.3.Final] at org.hibernate.event.internal.AbstractFlushingEventListener.flushEverythingToExecutions(AbstractFlushingEventListener.java:85) ~[hibernate-core-5.0.3.Final.jar:5.0.3.Final] at org.hibernate.event.internal.DefaultAutoFlushEventListener.onAutoFlush(DefaultAutoFlushEventListener.java:44) ~[hibernate-core-5.0.3.Final.jar:5.0.3.Final] at org.hibernate.internal.SessionImpl.autoFlushIfRequired(SessionImpl.java:1193) ~[hibernate-core-5.0.3.Final.jar:5.0.3.Final] at org.hibernate.internal.SessionImpl.list(SessionImpl.java:1261) ~[hibernate-core-5.0.3.Final.jar:5.0.3.Final] at org.hibernate.internal.QueryImpl.list(QueryImpl.java:87) ~[hibernate-core-5.0.3.Final.jar:5.0.3.Final] at org.hibernate.jpa.internal.QueryImpl.list(QueryImpl.java:567) ~[hibernate-entitymanager-5.0.3.Final.jar:5.0.3.Final] at org.hibernate.jpa.internal.QueryImpl.getSingleResult(QueryImpl.java:482) ~[hibernate-entitymanager-5.0.3.Final.jar:5.0.3.Final] at org.hibernate.jpa.criteria.compile.CriteriaQueryTypeQueryAdapter.getSingleResult(CriteriaQueryTypeQueryAdapter.java:54) ~[hibernate-entitymanager-5.0.3.Final.jar:5.0.3.Final] at org.springframework.data.jpa.repository.query.JpaQueryExecution$SingleEntityExecution.doExecute(JpaQueryExecution.java:206) ~[spring-data-jpa-1.9.0.RELEASE.jar:na] at org.springframework.data.jpa.repository.query.JpaQueryExecution.execute(JpaQueryExecution.java:78) ~[spring-data-jpa-1.9.0.RELEASE.jar:na] at org.springframework.data.jpa.repository.query.AbstractJpaQuery.doExecute(AbstractJpaQuery.java:100) ~[spring-data-jpa-1.9.0.RELEASE.jar:na] at org.springframework.data.jpa.repository.query.AbstractJpaQuery.execute(AbstractJpaQuery.java:91) ~[spring-data-jpa-1.9.0.RELEASE.jar:na] at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.doInvoke(RepositoryFactorySupport.java:454) ~[spring-data-commons-1.11.0.RELEASE.jar:na] at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.invoke(RepositoryFactorySupport.java:432) ~[spring-data-commons-1.11.0.RELEASE.jar:na] at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) ~[spring-aop-4.2.2.RELEASE.jar:4.2.2.RELEASE] at org.springframework.data.projection.DefaultMethodInvokingMethodInterceptor.invoke(DefaultMethodInvokingMethodInterceptor.java:61) ~[spring-data-commons-1.11.0.RELEASE.jar:na] at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) ~[spring-aop-4.2.2.RELEASE.jar:4.2.2.RELEASE] at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:99) ~[spring-tx-4.2.2.RELEASE.jar:4.2.2.RELEASE] at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:281) ~[spring-tx-4.2.2.RELEASE.jar:4.2.2.RELEASE] at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96) ~[spring-tx-4.2.2.RELEASE.jar:4.2.2.RELEASE] at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) ~[spring-aop-4.2.2.RELEASE.jar:4.2.2.RELEASE] at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:136) ~[spring-tx-4.2.2.RELEASE.jar:4.2.2.RELEASE] at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) ~[spring-aop-4.2.2.RELEASE.jar:4.2.2.RELEASE] at org.springframework.data.jpa.repository.support.CrudMethodMetadataPostProcessor$CrudMethodMetadataPopulatingMethodInterceptor.invoke(CrudMethodMetadataPostProcessor.java:101) ~[spring-data-jpa-1.9.0.RELEASE.jar:na] at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) ~[spring-aop-4.2.2.RELEASE.jar:4.2.2.RELEASE] at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92) ~[spring-aop-4.2.2.RELEASE.jar:4.2.2.RELEASE] at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) ~[spring-aop-4.2.2.RELEASE.jar:4.2.2.RELEASE] at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:207) ~[spring-aop-4.2.2.RELEASE.jar:4.2.2.RELEASE] at com.sun.proxy.$Proxy474.findByLogin(Unknown Source) ~[na:na] at point.office.CurrentUserImpl.get(CurrentUserImpl.java:42) ~[bin/:na] 字段更新任何实体时,exception就会发生。

前100行堆栈跟踪日志:

AuditorAware

我的@Component public class SpringSecurityAuditorAware implements AuditorAware<Employee> { private final CurrentUser currentUser; @Autowired public SpringSecurityAuditorAware(CurrentUser currentUser) { this.currentUser = currentUser; } @Override public Employee getCurrentAuditor() { return currentUser.get(); } } 实施是:

CurrentUser

我的@Service public class CurrentUserImpl implements CurrentUser { private final EmployeeRepository employeeRepository; @Autowired public CurrentUserImpl(EmployeeRepository employeeRepository) { this.employeeRepository = employeeRepository; } @Override public Employee get() { Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); if (authentication == null || !authentication.isAuthenticated()) { return null; } return employeeRepository.findByLogin(authentication.getName()).orElseGet(() -> null); } } 服务实现是:

EmployeeRepository

我的public interface EmployeeRepository extends JpaRepository<Employee, Integer> { Optional<Employee> findByLogin(String login); } 是:

@CreatedBy

我发生异常时尝试更新的实体可以是具有由spring @Entity @NoArgsConstructor @Getter @Setter public class Issue extends AbstractPersistable<Integer> { @CreatedDate @Column(nullable = false) private LocalDateTime createdAt; @CreatedBy @ManyToOne(optional = false) private Employee createdBy; @Column(nullable = true) private String body; } 注释的字段的任何实体。例如:

Issue issue = issueRepository.findOne(1);
issue.setBody("new issue body");
issueRepository.saveAndFlush(issue);

如果对于这个实体,我会这样做:

@Transactional(propagation = Propagation.REQUIRES_NEW)

然后发生异常。

我找到了一种解决方法:如果我将Employee get()添加到我的adb connect 192.168.0.100 adb shell wm overscan reset 方法,那么一切都开始有效。但是为什么没有呢?也许春季数据中存在错误?或者我做错了什么?

1 个答案:

答案 0 :(得分:2)

我会从Spring上下文中获取当前审核员,而不是查询它,例如:

@Component
public class SpringSecurityAuditorAware implements AuditorAware<Long> {

    @Override
    public Long getCurrentAuditor() {
        Long userId = 0L;   
        SecurityContext securityContext = SecurityContextHolder.getContext();   
        if ( securityContext.getAuthentication() != null ) {
            User user = (User) securityContext.getAuthentication().getPrincipal();
            userId = Long.valueOf(user.getId()); 
        }
        return userId;
    }

}