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.