@PreUpdate上的Stackoverflow

时间:2019-01-04 14:55:47

标签: java spring jpa

每当我尝试在实体上使用@PreUpdate批注时,都会遇到stackoverflow异常,该批注使用Embedded Auditable Entity和@EntityListeners。

在我的实体中,我试图通过服务获取用户数据,以便更新实体上的字段CreateBy / LastUpdateBy。我不能只是简单地将String放入其中,我需要引用User本身。

我已经尝试过将实体管理器注入AuditListener并手动刷新它的方法,但是它不能正常工作。另外,我尝试在spring上下文中传递User数据而不是使用服务,但是由于它保留了静态值,因此对我没有用。我需要以某种方式在AuditListener中获取User实体,而不必进入无限循环。

有什么想法吗?

这是堆栈跟踪:

2019-01-04 15:45:52 ERROR 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.lang.ClassLoader.defineClass1(Native Method)
                at java.lang.ClassLoader.defineClass(ClassLoader.java:763)
                at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
                at java.net.URLClassLoader.defineClass(URLClassLoader.java:468)
                at java.net.URLClassLoader.access$100(URLClassLoader.java:74)
                at java.net.URLClassLoader$1.run(URLClassLoader.java:369)
                at java.net.URLClassLoader$1.run(URLClassLoader.java:363)
                at java.security.AccessController.doPrivileged(Native Method)
                at java.net.URLClassLoader.findClass(URLClassLoader.java:362)
                at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
                at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:349)
                at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
                at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:147)
                at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
                at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:99)
                at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:282)
                at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96)
                at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
                at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:671)
                at com.app.server.repositories.impl.UserRepositoryImpl$$EnhancerBySpringCGLIB$$d072e6d0.findByUsername(<generated>)
                at com.app.server.services.impl.UserServiceImpl.findUserByUsername(UserServiceImpl.java:131)
                at com.app.server.services.impl.UserServiceImpl$$FastClassBySpringCGLIB$$852b5ce2.invoke(<generated>)
                at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204)
                at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:667)
                at com.app.server.services.impl.UserServiceImpl$$EnhancerBySpringCGLIB$$80381f41.findUserByUsername(<generated>)
                at com.app.server.defautmodel.audit.UserTimeAuditListener.setUpdatedOn(UserTimeAuditListener.java:37)
                at sun.reflect.GeneratedMethodAccessor177.invoke(Unknown Source)
                at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
                at java.lang.reflect.Method.invoke(Method.java:498)
                at org.hibernate.jpa.event.internal.ListenerCallback.performCallback(ListenerCallback.java:35)
                at org.hibernate.jpa.event.internal.CallbackRegistryImpl.callback(CallbackRegistryImpl.java:97)
                at org.hibernate.jpa.event.internal.CallbackRegistryImpl.preUpdate(CallbackRegistryImpl.java:71)
                at org.hibernate.event.internal.DefaultFlushEntityEventListener.invokeInterceptor(DefaultFlushEntityEventListener.java:350)
                at org.hibernate.event.internal.DefaultFlushEntityEventListener.handleInterception(DefaultFlushEntityEventListener.java:332)
                at org.hibernate.event.internal.DefaultFlushEntityEventListener.scheduleUpdate(DefaultFlushEntityEventListener.java:283)
                at org.hibernate.event.internal.DefaultFlushEntityEventListener.onFlushEntity(DefaultFlushEntityEventListener.java:154)
                at org.hibernate.event.internal.AbstractFlushingEventListener.flushEntities(AbstractFlushingEventListener.java:235)
                at org.hibernate.event.internal.AbstractFlushingEventListener.flushEverythingToExecutions(AbstractFlushingEventListener.java:94)
                at org.hibernate.event.internal.DefaultAutoFlushEventListener.onAutoFlush(DefaultAutoFlushEventListener.java:44)
                at org.hibernate.internal.SessionImpl.autoFlushIfRequired(SessionImpl.java:1415)
                at org.hibernate.internal.SessionImpl.list(SessionImpl.java:1501)
                at org.hibernate.query.internal.AbstractProducedQuery.doList(AbstractProducedQuery.java:1537)
                at org.hibernate.query.internal.AbstractProducedQuery.list(AbstractProducedQuery.java:1505)
                at org.hibernate.query.Query.getResultList(Query.java:135)
                at org.hibernate.query.criteria.internal.compile.CriteriaQueryTypeQueryAdapter.getResultList(CriteriaQueryTypeQueryAdapter.java:74)
                at com.app.server.repositories.impl.UserRepositoryImpl.findByUsername(UserRepositoryImpl.java:40)
                at com.app.server.repositories.impl.UserRepositoryImpl$$FastClassBySpringCGLIB$$46c3b507.invoke(<generated>)
                at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204)
                at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:736)
                at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)
                at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:136)
                at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
                at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:99)
                at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:282)
                at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96)
                at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
                at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:671)
                at com.app.server.repositories.impl.UserRepositoryImpl$$EnhancerBySpringCGLIB$$d072e6d0.findByUsername(<generated>)
                at com.app.server.services.impl.UserServiceImpl.findUserByUsername(UserServiceImpl.java:131)
                at com.app.server.services.impl.UserServiceImpl$$FastClassBySpringCGLIB$$852b5ce2.invoke(<generated>)
                at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204)
                at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:667)
                at com.app.server.services.impl.UserServiceImpl$$EnhancerBySpringCGLIB$$80381f41.findUserByUsername(<generated>)
                at com.app.server.defautmodel.audit.UserTimeAuditListener.setUpdatedOn(UserTimeAuditListener.java:37)
                at sun.reflect.GeneratedMethodAccessor177.invoke(Unknown Source)
                at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
                at java.lang.reflect.Method.invoke(Method.java:498)
                at org.hibernate.jpa.event.internal.ListenerCallback.performCallback(ListenerCallback.java:35)
                at org.hibernate.jpa.event.internal.CallbackRegistryImpl.callback(CallbackRegistryImpl.java:97)
                at org.hibernate.jpa.event.internal.CallbackRegistryImpl.preUpdate(CallbackRegistryImpl.java:71)
                at org.hibernate.event.internal.DefaultFlushEntityEventListener.invokeInterceptor(DefaultFlushEntityEventListener.java:350)
                at org.hibernate.event.internal.DefaultFlushEntityEventListener.handleInterception(DefaultFlushEntityEventListener.java:332)
                at org.hibernate.event.internal.DefaultFlushEntityEventListener.scheduleUpdate(DefaultFlushEntityEventListener.java:283)
                at org.hibernate.event.internal.DefaultFlushEntityEventListener.onFlushEntity(DefaultFlushEntityEventListener.java:154)
                at org.hibernate.event.internal.AbstractFlushingEventListener.flushEntities(AbstractFlushingEventListener.java:235)
                at org.hibernate.event.internal.AbstractFlushingEventListener.flushEverythingToExecutions(AbstractFlushingEventListener.java:94)
                at org.hibernate.event.internal.DefaultAutoFlushEventListener.onAutoFlush(DefaultAutoFlushEventListener.java:44)
                at org.hibernate.internal.SessionImpl.autoFlushIfRequired(SessionImpl.java:1415)
                at org.hibernate.internal.SessionImpl.list(SessionImpl.java:1501)
                at org.hibernate.query.internal.AbstractProducedQuery.doList(AbstractProducedQuery.java:1537)
                at org.hibernate.query.internal.AbstractProducedQuery.list(AbstractProducedQuery.java:1505)
                at org.hibernate.query.Query.getResultList(Query.java:135)
                at org.hibernate.query.criteria.internal.compile.CriteriaQueryTypeQueryAdapter.getResultList(CriteriaQueryTypeQueryAdapter.java:74)
                at com.app.server.repositories.impl.UserRepositoryImpl.findByUsername(UserRepositoryImpl.java:40)
                at com.app.server.repositories.impl.UserRepositoryImpl$$FastClassBySpringCGLIB$$46c3b507.invoke(<generated>)
                at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204)
                at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:736)
                at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)
                at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:136)
                at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
                at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:99)
                at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:282)
                at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96)
                at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
                at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:671)
                at com.app.server.repositories.impl.UserRepositoryImpl$$EnhancerBySpringCGLIB$$d072e6d0.findByUsername(<generated>)
                at com.app.server.services.impl.UserServiceImpl.findUserByUsername(UserServiceImpl.java:131)
                at com.app.server.services.impl.UserServiceImpl$$FastClassBySpringCGLIB$$852b5ce2.invoke(<generated>)
                at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204)
                at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:667)
                at com.app.server.services.impl.UserServiceImpl$$EnhancerBySpringCGLIB$$80381f41.findUserByUsername(<generated>)
                at com.app.server.defautmodel.audit.UserTimeAuditListener.setUpdatedOn(UserTimeAuditListener.java:37)
                at sun.reflect.GeneratedMethodAccessor177.invoke(Unknown Source)
                at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
                at java.lang.reflect.Method.invoke(Method.java:498)
                at org.hibernate.jpa.event.internal.ListenerCallback.performCallback(ListenerCallback.java:35)
                at org.hibernate.jpa.event.internal.CallbackRegistryImpl.callback(CallbackRegistryImpl.java:97)
                at org.hibernate.jpa.event.internal.CallbackRegistryImpl.preUpdate(CallbackRegistryImpl.java:71)
                at org.hibernate.event.internal.DefaultFlushEntityEventListener.invokeInterceptor(DefaultFlushEntityEventListener.java:350)
                at org.hibernate.event.internal.DefaultFlushEntityEventListener.handleInterception(DefaultFlushEntityEventListener.java:332)
                at org.hibernate.event.internal.DefaultFlushEntityEventListener.scheduleUpdate(DefaultFlushEntityEventListener.java:283)
                at org.hibernate.event.internal.DefaultFlushEntityEventListener.onFlushEntity(DefaultFlushEntityEventListener.java:154)
                at org.hibernate.event.internal.AbstractFlushingEventListener.flushEntities(AbstractFlushingEventListener.java:235)
                at org.hibernate.event.internal.AbstractFlushingEventListener.flushEverythingToExecutions(AbstractFlushingEventListener.java:94)
                at org.hibernate.event.internal.DefaultAutoFlushEventListener.onAutoFlush(DefaultAutoFlushEventListener.java:44)
                at org.hibernate.internal.SessionImpl.autoFlushIfRequired(SessionImpl.java:1415)
                at org.hibernate.internal.SessionImpl.list(SessionImpl.java:1501)
                at org.hibernate.query.internal.AbstractProducedQuery.doList(AbstractProducedQuery.java:1537)
                at org.hibernate.query.internal.AbstractProducedQuery.list(AbstractProducedQuery.java:1505)
                at org.hibernate.query.Query.getResultList(Query.java:135)
                at org.hibernate.query.criteria.internal.compile.CriteriaQueryTypeQueryAdapter.getResultList(CriteriaQueryTypeQueryAdapter.java:74)
                at com.app.server.repositories.impl.UserRepositoryImpl.findByUsername(UserRepositoryImpl.java:40)
                at com.app.server.repositories.impl.UserRepositoryImpl$$FastClassBySpringCGLIB$$46c3b507.invoke(<generated>)
                at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204)
                at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:736)
                at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)
                at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:136)
                at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
                at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:99)
                at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:282)
                at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96)
                at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
                at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:671)
                at com.app.server.repositories.impl.UserRepositoryImpl$$EnhancerBySpringCGLIB$$d072e6d0.findByUsername(<generated>)
                at com.app.server.services.impl.UserServiceImpl.findUserByUsername(UserServiceImpl.java:131)
                at com.app.server.services.impl.UserServiceImpl$$FastClassBySpringCGLIB$$852b5ce2.invoke(<generated>)
                at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204)
                at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:667)
                at com.app.server.services.impl.UserServiceImpl$$EnhancerBySpringCGLIB$$80381f41.findUserByUsername(<generated>)
                at com.app.server.defautmodel.audit.UserTimeAuditListener.setUpdatedOn(UserTimeAuditListener.java:37)
                at sun.reflect.GeneratedMethodAccessor177.invoke(Unknown Source)
                at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
                at java.lang.reflect.Method.invoke(Method.java:498)
                at org.hibernate.jpa.event.internal.ListenerCallback.performCallback(ListenerCallback.java:35)
                at org.hibernate.jpa.event.internal.CallbackRegistryImpl.callback(CallbackRegistryImpl.java:97)
                at org.hibernate.jpa.event.internal.CallbackRegistryImpl.preUpdate(CallbackRegistryImpl.java:71)
                at org.hibernate.event.internal.DefaultFlushEntityEventListener.invokeInterceptor(DefaultFlushEntityEventListener.java:350)
                at org.hibernate.event.internal.DefaultFlushEntityEventListener.handleInterception(DefaultFlushEntityEventListener.java:332)
                at org.hibernate.event.internal.DefaultFlushEntityEventListener.scheduleUpdate(DefaultFlushEntityEventListener.java:283)
                at org.hibernate.event.internal.DefaultFlushEntityEventListener.onFlushEntity(DefaultFlushEntityEventListener.java:154)
                at org.hibernate.event.internal.AbstractFlushingEventListener.flushEntities(AbstractFlushingEventListener.java:235)
                at org.hibernate.event.internal.AbstractFlushingEventListener.flushEverythingToExecutions(AbstractFlushingEventListener.java:94)
                at org.hibernate.event.internal.DefaultAutoFlushEventListener.onAutoFlush(DefaultAutoFlushEventListener.java:44)

1 个答案:

答案 0 :(得分:0)

默认情况下,所有交易都是read/write,并且由于您使用的是@PreUpdate,因此FlushMode交易中的read/write总是会触发所有交易。在您的情况下,如果您只想获取数据时需要绕过@PreUpdate,则您需要为交易指定的数据至少为readOnly,这将在FlushMode.NEVER中创建休眠会话,因此{ {1}}将不会被触发。为了更加安全,以防您的事务被嵌入pervios事务中,因此它将继承其属性,您可以使用@PreUpdate来暂停先前的事务执行,但是请谨慎使用,因为这可能会导致大型应用程序出现性能问题