I take my previous statement back. Here is a simplified view of what causes the ConcurrentModificationException (CME):
Code:
Session session = sessionFactory.openSession();
MyProfile profile = (MyProfile) session.load(MyProfile.class, 1234);
//profile.getFirstName();
String entityName = session.getEntityName(profile);
If the commented line in the middle (profile.getFirstName()) remains commented the proxy has not been initialized with data from the database. Calling session.getEntityName(profile) causes the lazy initialization to happen while a collection is being iterated over (and a CME is thrown).
If the line is uncommented getFirstName() causes the lazy initialization to occur. Calling getEntityName(profile) now results in:
Code:
org.hibernate.TransientObjectException: proxy was not associated with the session
at org.hibernate.impl.SessionImpl.getEntityName(SessionImpl.java:1738)
This is very strange to me because I just used the session to load that proxy! Why is this happening?