简体   繁体   English

Java 迭代集合在保存后抛出 ConcurrentModificationException

[英]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.

相关问题 迭代优先级队列引发ConcurrentModificationException - iterating priority queue throws ConcurrentModificationException 迭代ArrayList时Java ConcurrentModificationException - Java ConcurrentModificationException when iterating ArrayList 在ConcurrentModificationException之后,Java集合是否保证处于有效,可用的状态? - Is a Java collection guaranteed to be in a valid, usable state after a ConcurrentModificationException? 迭代此集合时如何避免ConcurrentModificationException? - How to avoid ConcurrentModificationException while iterating this collection? Java同级移除会引发ConcurrentModificationException - Java sibling removal throws ConcurrentModificationException 抛出java.util.ConcurrentModificationException - Throws java.util.ConcurrentModificationException Java forEach lambda引发并发ModificationException - Java forEach lambda throws concurrentModificationException java.util.ConcurrentModificationException,但我正在迭代一个副本,而不是我正在修改的集合 - java.util.ConcurrentModificationException, but I'm iterating over a copy, not the collection I'm modifying 如果在迭代时修改集合而不抛出 ConcurrentModificationException 会发生什么 - what will happen if modify a collection while iterating without throwing the ConcurrentModificationException 遍历集合,在循环中删除对象时避免 ConcurrentModificationException - Iterating through a Collection, avoiding ConcurrentModificationException when removing objects in a loop
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM