0

When I save a list of added elements I get a concurrent modification exception. Below is the code of the save method called:

listAta listata = em.findbyid(id);
for (adressata ad : getInstance().getList()) {
    em.merge(ad);
}
em.merge(listata);
em.flush();

The line that caused the exception is for (adressata ad : getInstance().getList())

Below is the entity data:

@ManyToOne((fetch = FetchType.LAZY, cascade = {CascadeType.PERSIST, CascadeType.MERGE})
@JoinColumn(name="id_list" , nullable=false)
private ListAta listAta;

How is this caused and how can I solve it?

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
pirate dev
  • 21
  • 5
  • the line causing the problem is `em.merge(ad);` (it's modifying the list that is (con)currently being iterated), the one complaining it the for loop like explained in this related question: http://stackoverflow.com/questions/1496180/concurrent-modification-exception – zapl Jun 01 '16 at 11:08
  • sorry but i don't undrestand the solution gived in http://stackoverflow.com/questions/1496180/concurrent-modification-exception – pirate dev Jun 01 '16 at 11:12
  • What is `getInstance().getList()`? How is it related to `adressata` and `ListAta`? Does changing the for loop to `for (adressata ad : new ArrayList<>(getInstance().getList()))` help? (there is a chance you have an actual multi threaded problem, maybe the related question isn't so much related as I thought) – zapl Jun 01 '16 at 11:23
  • You have added CascadeType.MERGE to the association so (if the rest of your code is correct) you do not need to call em.merge(ad) for each individual item. What problem are you trying to solve? – Alan Hay Jun 01 '16 at 11:24
  • Alan Hay : i use em.merge(ad) to update the entity after modification – pirate dev Jun 01 '16 at 11:26
  • zapl : the getInstance().getList() will get the list of adressata of listata – pirate dev Jun 01 '16 at 11:28

0 Answers0