What is the best practice for iterating nested collections.
I get ConcurrentModificationException when attempted to iterate a nested
collection.
I don't understand.
Thanks in advance.
3.05:
Mapping documents:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.tests.A">
<id name="orderNumber" column="orderNumber" type="string">
<generator class="assigned"/>
</id>
<list name="bList" cascade="all" lazy="true">
<key column="parentId"/>
<list-index column="collIndex"/>
<many-to-many class="com.htg.tests.B"/>
</list>
</class>
</hibernate-mapping>
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.htg.tests.B">
<id name="Key" column="id" type="string">
<generator class="uuid.hex"/>
</id>
<list name="cList" cascade="all" lazy="true">
<key column="parentId"/>
<list-index column="collIndex"/>
<many-to-many class="com.tests.C"/>
</list>
</class>
</hibernate-mapping>
<hibernate-mapping>
<id name="Key" column="id" type="string">
<generator class="uuid.hex"/>
</id>
<class name="com.htg.tests.C">
<property name="state"/>
</class>
</hibernate-mapping>
l
tx = sess.beginTransaction();
List aColl = sess.createQuery("from A as order").list();
for(int i = 0;i< aColl.size();i++){
A a = (A) aColl.get(i);
List bColl = a.getBList();
for(int j = 0;j< bColl.size();j++){
B b = (B) bColl.get(i);
List cColl = b.getCList();
for(int k = 0;k< cColl.size();k++){
C c = (C) cColl.get(k);
if(c.getState() == true){
break;//: This results in a ConcurrentModificationException
//: When the transaction is committed.
}
}
}
}
Full stack trace of any exception that occurs:
Root cause:
java.util.ConcurrentModificationException
at java.util.HashMap$HashIterator.nextEntry(HashMap.java:782)
at java.util.HashMap$ValueIterator.next(HashMap.java:812)
at org.hibernate.pretty.Printer.toString(Printer.java:90)
at org.hibernate.event.def.AbstractFlushingEventListener.flushEverythingToExecutions(AbstractFlushingEventListener.java:91)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:26)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:730)
at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:324)
at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:86)
Name and version of the database you are using:
MySQL 5.0
The generated SQL (show_sql=true):
Debug level Hibernate log excerpt: Code:
Code: