IT/자바, 스프링
자바 for문 돌리는 중 ConcurrentModificationException 에러
thesse
2022. 1. 13. 16:48
300x250
반응형
for(Stroe store : user.getStoreList()){
...
store.setActive(true);
storeRepository.save(store);
}
위와 같은 반복문을 돌리고 있는데
처음 한 번은 잘 돌더니 두번째에서 ConcurrentModificationException 에러가 발생했다.
java.util.ConcurrentModificationException: null
at java.base/java.util.ArrayList$Itr.checkForComodification(ArrayList.java:1043) ~[na:na]
at java.base/java.util.ArrayList$Itr.next(ArrayList.java:997) ~[na:na]
at org.hibernate.collection.internal.AbstractPersistentCollection$IteratorProxy.next(AbstractPersistentCollection.java:887) ~[hibernate-core-5.4.32.Final.jar:5.4.32.Final]
...
store를 조건으로 반복문을 돌리고 있는데
반복 와중에 대상이 되는 store 엔티티가 db상에서 변경되어 null이 뜨는 것이다.
db 객체를 바로 사용하지 않고 별도의 List로 만들어 for문의 조건에 사용하니 에러 안뜨고 잘 돌아감
for(Stroe store : user.getStoreList().stream().collect(Collectors.toList()){
...
store.setActive(true);
storeRepository.save(store);
}
300x250
반응형