In Hibernate version 2.1.2, is it a no-no to modify a set that is lazily loaded and cascaded using an
Iterator? Here is my setup...
Class Foo has a set of Bars. The 'bars' set is lazily loaded and cascaded as shown below.
Code:
<hibernate-mapping>
<class
name="Foo"
dynamic-update="false"
dynamic-insert="false"
>
<set
name="bars"
lazy="true"
inverse="true"
cascade="all-delete-orphan"
>
<key
column="foo_id"
/>
<one-to-many
class="Bar"
/>
</set>
</hibernate-mapping>
If I run code like this:
Code:
Set bars = someFoo.getBars();
Iterator it = bars.iterator();
while (it.hasNext())
{
Bar bar = (Bar)it.next();
bars.remove(bar);
session.delete(bar);
}
On the second pass through the loop the call to
it.next() throws a
ConcurrentModificationException. If I instead create a List from the set of bars and iterate through the list all works well. Shown below.
Code:
Set bars = someFoo.getBars();
List l = new ArrayList(bars);
for (int i=0; i<l.size(); i++)
{
Bar bar = (Bar)l.get(i);
bars.remove(bar);
session.delete(bar);
}
Thanks,
Rob