java – Spring JPA deleteInBatch导致StackOverflow

前端之家收集整理的这篇文章主要介绍了java – Spring JPA deleteInBatch导致StackOverflow前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

我有一个问题是使用deleteInBatch从db中删除项目.
我有一个对象A有一个对象B列表,如:

class A {
private List 

该列表包含7k多个元素.
所以现在我必须删除A及其所有元素.我试过通过deleteInBatch,但我得到了

org.springframework.web.util.NestedServletException: Handler processing Failed;
nested exception is java.lang.StackOverflowError

使用sipmle delete方法删除项目有效,但需要5分钟以上.
我的删除代码是:

public void delete(Long id) {
A a = repository.findOne(id);
deleteElements(a);
repository.delete(a);
}

private void deleteElements(A a) {
repository.deleteInBatch(a.getListOfB);
}

有没有一个很好的解决方案来加快删除过程或如何更改,以便deleteinbatch不占用所有休眠堆栈 – 不增加它?

完整的Stacktrace:

org.springframework.web.util.NestedServletException: Handler processing Failed; nested exception is java.lang.StackOverflowError
org.springframework.web.servlet.DispatcherServlet.triggerAfterCompletionWithError(DispatcherServlet.java:1259)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:945)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:856)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:936)
org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:827)
javax.servlet.http.HttpServlet.service(HttpServlet.java:620)
org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:812)
javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
net.bull.javamelody.MonitoringFilter.doFilter(MonitoringFilter.java:163)
net.bull.javamelody.MonitoringFilter.doFilter(MonitoringFilter.java:206)
net.bull.javamelody.MonitoringFilter.doFilter(MonitoringFilter.java:179)
org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:330)
org.springframework.security.web.access.intercept.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:118)
org.springframework.security.web.access.intercept.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java:84)
org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
org.springframework.security.web.access.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:113)

等等…

root cause

java.lang.StackOverflowError
org.hibernate.hql.internal.ast.tree.sqlNode.sqlNode.java:34)
sun.reflect.GeneratedConstructorAccessor36.newInstance(Unknown Source)
sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
java.lang.reflect.Constructor.newInstance(Constructor.java:526)
java.lang.Class.newInstance(Class.java:379)
org.hibernate.hql.internal.ast.sqlASTFactory.create(sqlASTFactory.java:256)
antlr.ASTFactory.create(ASTFactory.java:153)
antlr.ASTFactory.create(ASTFactory.java:186)
org.hibernate.hql.internal.antlr.HqlsqlBaseWalker.logicalExpr(HqlsqlBaseWalker.java:2018)
org.hibernate.hql.internal.antlr.HqlsqlBaseWalker.logicalExpr(HqlsqlBaseWalker.java:2026)
org.hibernate.hql.internal.antlr.HqlsqlBaseWalker.logicalExpr(HqlsqlBaseWalker.java:2026)

等等….

最佳答案
在您的情况下,删除查询将由JpaRepository转换为如下所示.

delete from [table_name] where [criteria] = id or [criteria] = id (and so on...)

jvm抛出堆栈溢出错误,因为HqlsqlBaseWalker正在尝试搜索所有或(或基本上where标准)语句

我想,在你的情况下,你可以尝试生成自己的删除查询,然后执行它,或者你可以尝试将数据拆分成几个列表.

猜你在找的Spring相关文章