| We would still have the problem when the Entity is associated with more than one PersistenceContext. With enhanced Entities, we store the ManagedEntity state within the Entity itself, there is a one-to-one mapping between an Entity and the ManagedEntity state. Even if we can check to see if the ManagedEntity state relates to the current PersistenceContext, we would still have the problem that if the enhanced Entity becomes associated with another open session, the ManagedEntity state from the first session will be overwritten with the ManagedEntity state from the second session. I think the enhanced entity needs a map between the PersistenceContext and ManagedEntity so multiple ManageEntity states can be stored in an enhanced Entity. However, that requires an API for change. The way of handling it is to say that an enhanced entity can only be associated with a single open session, and throw an exception when a user tries to associate the Entity with a second open session. That might break user applications though, and render enhanced entities unusable for some user applications. The case of Enhanced Immutbale Entites is slightly different. The purpose of immutable Entities is so that we can share the EntityEntry state, between sessions and save on heap pressure. For ImmutableEntityEntry the persistence state should be Immutable after it has been loaded into memory, and shared between sessions. Even if multiple sessions try to overwrite the EntityEntry, it will be the same information, so we handle this differently. This issue with ImmutableEntityEntry will be with the linking ManagedEntity.$$_hibernate_getNextManagedEntity() and ManagedEntity.$$_hibernate_getPreviousManagedEntity(). I was going to open another issue to deal with this, but I see you have already created https://hibernate.atlassian.net/browse/HHH-10903 |