StatefulPersistenceContext. getEntry( Object entity) returns the EntityEntry that has been cached in the enhanced ManagedEntity, so we need to check that the cached EntityEntry has been added by the current PersistenceContext or a different PersistenceContext. If we can have an ManagedEntity in multiple sessions each with their own EntityEntries, is it correct to have a one-to-one mapping between ManagedEntity and EntityEntry, e.g. ManagedEntity.$$_hibernate_getEntityEntry() returns a single EntityEntry. Would it be better represented by a Map , so ManagedEntity.$$_hibernate_getEntityEntry(PersistenceContext persistenceContext) would return the correct EntityEntry object. This would be an API change though, so not backward compatible. We can not test the EntityEntry to see which PersistenceContext it belongs to, and check that the current PersistenceContext is equal to the PersistenceConect referenced EntityEntry, the EntityEntry API does not expose the information, but even if we could, as soon as we added the ManagedEntity to a second session, the EntityEntry reference in the ManagedEntity would be overwritten and we would loose the reference to the first session that references the ManagedEntity. |