I got the following exception :
java.util.ConcurrentModificationException
        at java.util.HashMap$HashIterator.nextEntry(HashMap.java:793)
        at java.util.HashMap$KeyIterator.next(HashMap.java:828)
        at org.hibernate.collection.AbstractPersistentCollection$IteratorProxy.next(AbstractPersistentCollection.java:580)
        at org.xxxxx.yyyyyy.NetGroupPeerTest.updateGroupP(NetGroupPTest.java:265)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
This error occurs during JUNIT test for a class that uses many-to-many relationship implemented by an association class (table)
We are inside the scope of the session under JUNIT framework.
Involved code is : 
    @Test
    public void updateGroupP() {
        try {
            Query q = session.createQuery("from NetGroup gpp where gpp.netId = :netid");
            NetGroup group1 = (NetGroup) q.setString("netid", "uri:000001").uniqueResult();
            Set<NetGroupP> gpp = group1.getGroupP();
            for(NetGroupP elt : gpp) {
                    gpp.remove(elt);
                }
            }
The error occurs just after removing the elt from the collection. What is strange : removing the elt by implementing a method inside the owner class of the collection does not trigger the exception!?
 group1.removeP(elt);
 public NetGroup {
    public void removeP(NetP p) {
        for (NetGroupP gpp : groupPs) {
            if( gpp.getP().getPId() == p.getPId()) {
                groupP.remove(gpp);
            }
        }
    }
 }
So, to be brief, getting the collection via a getter and calling its method remove() triggers an ConcurrentModificationException but doing the "same thing" inside the class that holds the collection does not ! Note that the hascode() for the Set of the collection is changing after deleting an object from the collection. According to the JavaDoc, this behavior is normal :
From : 
http://download.oracle.com/javase/1.5.0 ... Code%28%29"Returns the hash code value for this set. The hash code of a set is defined to be the sum of the hash codes of the elements in the set, where the hashcode of a null element is defined to be zero. This ensures that s1.equals(s2) implies that s1.hashCode()==s2.hashCode() for any two sets s1 and s2, as required by the general contract of the Object.hashCode method."
Does anybody has a beginning of explanation ? since that apparently we are on the same thread, we got hopefully the same object and we are inside the same hibernate session.
Regards,