These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: ConcurrentModificationException Set / HashSet ->remove(obj)
PostPosted: Fri Dec 17, 2010 3:30 pm 
Newbie

Joined: Fri Dec 17, 2010 10:53 am
Posts: 8
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,


Top
 Profile  
 
 Post subject: Re: ConcurrentModificationException Set / HashSet ->remove(obj)
PostPosted: Fri Dec 17, 2010 4:09 pm 
Newbie

Joined: Fri Dec 17, 2010 10:53 am
Posts: 8
I think I've found an explanation, at least a beginning...! that makes me to laugh about me.... It is because I'm cutting the branch were I'm sitting on... I mean the code is deleting an elt from the collection which is itself include in a loop ! However, it doe not explain why the "same code" runs perfectly inside the owner class ! So, to resolve the problem from the external point of view, the objects to be deleted are collected and an another loop performs the delete operation.

Code:
            Set<NetGroupP> gpp = group1.getGroupP();

            List<NetP> pList = new Vector<NetP>();
            for(NetGroupP elt : gpp) {
               pList.add(elt.getP());
            }

            for (NetP pList : p)  {
                group1.removeP(p);
            }


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.