[英]Java iterating collection throws ConcurrentModificationException after Save
Hi I am using hibernate for saving the objects to DB.嗨,我正在使用 hibernate 将对象保存到数据库。 I have a Parent object and set of child objects under it.
我有一个父 object 和它下面的一组子对象。 While iterating over the child objects, if a specific criteria is matched then I publish an event using ApplicationEventPublisher, this event is handled separately, which adds few additional logics and saves the object.
在迭代子对象时,如果匹配特定条件,则我使用 ApplicationEventPublisher 发布事件,此事件将单独处理,这会添加一些额外的逻辑并保存 object。 After the control returns from event handler, it throws
"java.util.ConcurrentModificationException"
控件从事件处理程序返回后,抛出
"java.util.ConcurrentModificationException"
Below is the code,下面是代码,
parent.getChild().forEach(child -> {
if (child.getStatus().equals("PENDING")) {
applicationEventPublisher.publishEvent(new NewEvent(child));
publishLifeCycleEvent(deal);
}
}
});
There is a Event Listner,有一个事件监听器,
@EventListener
public void saveChildEvent(NewEvent childEvent) {
Child child = childEvent.getChild();
//...do processing
childRepository.save(child);
}
But it throws java.util.ConcurrentModificationException at this point.但此时它会抛出 java.util.ConcurrentModificationException 。 Any idea how to rectify this?
知道如何纠正这个问题吗? Any help is appreciated.
任何帮助表示赞赏。
Stack Trace -
java.util.ConcurrentModificationException: null
at java.base/java.util.HashMap$HashIterator.nextNode(HashMap.java:1493)
at java.base/java.util.HashMap$KeyIterator.next(HashMap.java:1516)
at org.hibernate.collection.internal.AbstractPersistentCollection$IteratorProxy.next(AbstractPersistentCollection.java:893)
at java.base/java.lang.Iterable.forEach(Iterable.java:74)
You can iterate over a copy of the list like this:您可以像这样遍历列表的副本:
var children = new ArrayList<>(parent.getChild());
children .forEach(child -> {
if (child.getStatus().equals("PENDING")) {
applicationEventPublisher.publishEvent(new NewEvent(child));
publishLifeCycleEvent(deal);
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.