Monthly Archives: December 2013

JPA java.util.ConcurrentModificationException on @manytomany List

I just had a java.util.ConcurrentModificationException when updating a List with JPA. Below the code which caused it, the required fix and some attention points.

@Table(name = "Beurs")
public class Beurs implements Serializable {
	private static final long serialVersionUID = -6250201709027758975L;

	@Column(name = "beurscode", unique = true, nullable = false)
    private Long beurscode;

    private String beursnaam;

    private Integer iexFondsCode;

    private String iexFondsnaam;

    @ManyToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
    @JoinTable(name = "BeursFonds"  , joinColumns = { @JoinColumn(name = "beurs_beurscode", nullable = false, updatable = true) }
                                    , inverseJoinColumns = { @JoinColumn(name = "fonds_fondscode", nullable = false, updatable = true) })
	private List<Fonds> fondsen;

@Table(name = "Fonds")
public class Fonds implements Serializable {
	private static final long serialVersionUID = 4655056015858729584L;

	@Column(name = "fondscode")
	private Long fondscode;

	@ManyToMany(mappedBy = "fondsen", fetch=FetchType.EAGER, cascade = CascadeType.ALL)
	private List<Beurs> beurzen;

The following code caused the exception, since while iterating over the List it is tried to update an entry of the same List. On some fora it is advised to use a ListIterator and call the add() and remove() functions on these.

	public void store(Fonds fonds) {
        List<Beurs> beurzen = fonds.getBeurzen();
        for(Beurs beurs: beurzen) {
            Beurs beursRepo = entityManager.find(Beurs.class, beurs.getBeurscode());


But since i also want an easy way to update existing objects in the array, I wrote the following code. First it updates the child [Fonds], then throws away all related entries in the jointable [if any] and then inserts the entries to the jointable.

Note that no update is being done on the parent [Beurs], so this one might be outdated on the first level cache. Especially the entries of the Fonds object. So in order to prevent first level cache issues, a flush() and a refresh() are done.


        // reset the manytomany

            for(Beurs beurs: fonds.getBeurzen()) {
       BeursFonds(beurs.getBeurscode(), fonds.getFondscode()));

                Beurs beursRepo = entityManager.find(Beurs.class, beurs.getBeurscode());


Finally, the original exception. Which we just fixed.

	at java.util.ArrayList$Itr.checkForComodification(
	at java.util.ArrayList$
	at org.hibernate.collection.AbstractPersistentCollection$
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(
	at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(
	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(
	at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(
	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(
	at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(
	at com.sun.proxy.$ Source)