-->
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.  [ 3 posts ] 
Author Message
 Post subject: ConcurrentModificationException when removing item from Set
PostPosted: Wed Aug 11, 2004 3:34 pm 
Newbie

Joined: Thu Apr 08, 2004 4:22 pm
Posts: 4
Hi All, I'm getting a ConcurrentModificationException when I'm trying to remove an item from a Set in a persisted class.

The scenario:

I load a persisted object from the database, close the session and put the detached object into httpSession. The object is then modified later, a "passenger" is removed from the "passengers" Set of a Carpool object, and the error occurs.

The database tables are Carpool and User with an assocation table called passenger. My goal is to just remove the link info in the passenger table. I've tried using all-delete-orphan, but that deletes the the entry in the User table--gosh, but I just don't want that to happen, no sir.

Tell me I'm missing some _extremely_ elementry item.

Thanks, info below.

John

Hibernate version:2

Mapping documents:
<?xml version="1.0"?>

<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">

<hibernate-mapping>
<class
name="com.wamu.carpool.Carpool"
table="carpool"
dynamic-update="false"
dynamic-insert="false"
>

<id
name="id"
column="id"
type="long"
unsaved-value="-1"
>
<generator class="net.sf.hibernate.id.TableHiLoGenerator">
<param name="table">Sequencer</param>
<param name="column">seed</param>
<param name="max_lo">10</param>
</generator>
</id>

<property
name="comments"
type="java.lang.String"
update="true"
insert="true"
access="property"
column="comments"
length="255"
/>

<property
name="capacity"
type="int"
update="true"
insert="true"
access="property"
column="capacity"
/>

<property
name="departureTime"
type="java.util.Date"
update="true"
insert="true"
access="property"
column="departureTime"
/>

<many-to-one
name="driver"
class="com.wamu.carpool.security.User"
cascade="none"
outer-join="auto"
update="true"
insert="true"
access="property"
column="driverId"
/>

<many-to-one
name="exit"
class="com.wamu.carpool.Exit"
cascade="none"
outer-join="auto"
update="true"
insert="true"
access="property"
column="exitId"
/>

<set
name="passengers"
table="passenger"
lazy="false"
inverse="false"
cascade="all-delete-orphan"
sort="unsorted"
>

<key
column="carpoolId"
>
</key>

<many-to-many
class="com.wamu.carpool.security.User"
column="userId"
outer-join="auto"
/>

</set>

<!--
To add non XDoclet property mappings, create a file named
hibernate-properties-Carpool.xml
containing the additional properties and place it in your merge dir.
-->

</class>

</hibernate-mapping>


Code between sessionFactory.openSession() and session.close():
try {
Session session = HibernateSessionFactory.currentSession();

Transaction tx = session.beginTransaction();

//reassociate the carpool with the hibernate session.
Carpool refreshedCarpool = (Carpool)session.load(Carpool.class, new Long(carpool.getId()));

logger.debug("removing passenger " + passenger.getUserName());
logger.debug("passenger size:" + String.valueOf(refreshedCarpool.getPassengers().size()));
Iterator i = refreshedCarpool.getPassengers().iterator();

while(i.hasNext()){
User currentPassenger = (User)i.next();
if(currentPassenger.getUserName().equals(passenger.getUserName())){
refreshedCarpool.getPassengers().remove(currentPassenger);
}
}
logger.debug("passenger size after remove():" + String.valueOf(refreshedCarpool.getPassengers().size()));

session.update(refreshedCarpool);
tx.commit();
} catch (HibernateException e) {
logger.error(e);
throw new RuntimeException(e);
} finally {
try {
HibernateSessionFactory.closeSession();
} catch (HibernateException e1) {
logger.error(e1);
}
}

Full stack trace of any exception that occurs:
Caused by: java.util.ConcurrentModificationException
at java.util.HashMap$HashIterator.nextEntry(HashMap.java:782)
at java.util.HashMap$KeyIterator.next(HashMap.java:818)
at net.sf.hibernate.collection.PersistentCollection$IteratorProxy.next(PersistentCollection.java:441)
at com.wamu.carpool.CarpoolManagerHibernateImpl.removePassenger(CarpoolManagerHibernateImpl.java:142)
at com.wamu.carpool.CarpoolHandler.removePassenger(CarpoolHandler.java:255)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:324)
at com.sun.faces.el.MethodBindingImpl.invoke(MethodBindingImpl.java:126)
... 34 more

Name and version of the database you are using:
HSQL v1.7.2


Top
 Profile  
 
 Post subject: Found My Solution
PostPosted: Wed Aug 11, 2004 4:23 pm 
Newbie

Joined: Thu Apr 08, 2004 4:22 pm
Posts: 4
The problem is solved by overriding the equals() and hashcode() methods of Object. Hibernate uses these methods when doing it's internal methods. Once I did this, things went smoothly. Here's the change to my code:



Code:
  try {
            Session session = HibernateSessionFactory.currentSession();

            Transaction tx = session.beginTransaction();
           
            //reassociate the carpool with the hibernate session.
            session.lock(carpool,LockMode.NONE);
           
           
            logger.debug("removing passenger " + passenger.getUserName());
            logger.debug("passenger size:" + String.valueOf(carpool.getPassengers().size()));
         
            carpool.getPassengers().remove(passenger);
           
            logger.debug("passenger size after remove():" + String.valueOf(carpool.getPassengers().size()));
           
            session.update(carpool);
            tx.commit();
        } catch (HibernateException e) {
            logger.error(e);
            throw new RuntimeException(e);
        } finally {
            try {
                HibernateSessionFactory.closeSession();
            } catch (HibernateException e1) {
                logger.error(e1);
            }
        }


The moral of the story is to override the equals() and hashcode() methods when working with objects in a persisted Collection.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 11, 2004 4:25 pm 
Regular
Regular

Joined: Tue Oct 07, 2003 10:20 am
Posts: 77
The concurrent modification exception was a Java error - nothing to do with Hibernate, or the equals method, etc.

You can't remove an object from a Collection the way you were, when you're iterating through it. If you want to remove an object while iterating, use the remove method available on the iterator.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 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.