我想使用JPA持久化一個實體列表。我的第一種方法如下:錯誤持續使用java並行數據流的實體列表
List<myEntity>entityList=myService.getMyEntities();`
\\some other activities...`
entityList.parallelStream().forEach(l->l.setMyStatus(MyStatus.newStatus);
entityList.parallelStream().forEach(l->myService.getEntityManager().merge(l));
但我得到了一個OptimisticLockException。
我的第二種方法是使用流而不是並行流。
entityList.stream().forEach(l->myService.getEntityManager().merge(l));
第二種方法可行!現在我的問題是爲什麼parallelStream()會導致樂觀鎖定異常?
你的第二種方法仍然使用並行流。你的第一個包含編譯錯誤。 – Holger
@Holger很抱歉這是一個輸入錯誤。我糾正了它 – Lakshi