There will still be problems if a ManagedEntity that is still associated with a PersistenceContext is loaded from the reference cache into a different PersistenceContext. That ManagedEntity will still have the previous/next references from the original PersistenceContext. One idea is to wrap the immutable ManagedEntity objects with a ManagedEntityHolder that implements ManagedEntity, adding ManagedEntityHolder#getManagedEntity to retrieve the actual immutable ManagedEntity (with null previous/next references). Instead, ManagedEntityHolder would hold the previous/next references specific to that PersistenceContext. Immutable entities cannot be updated, but they can be created and deleted. The ManagedEntityHolder previous/next references are necessary so that EntityEntryContext#reentrantSafeEntityEntries returns the EntityEntry objects in the proper order to avoid constraint violations. This way EntityEntryContext can still determine if it contains a particular ManagedEntity by walking the EntityEntryContext#head/tail linked list. I imagine that maintaining a Set of immutable ManagedEntity objects in EntityEntryContext and checking that Set would perform better. If
HHH-10251 Closed is not reverted (i.e., so PersistenceContext is still inaccessible from ImmutableEntityEntry), I think that it would then be safe to have an immutable ManagedEntity} associated with multiple {{PersistenceContext.. I am not sure what will happen if an immutable ManagedEntity is deleted in a particular PersistenceContext when it is associated with another PersistenceContext. If that happens, then EntityEntry#setDeletedState will be called and that may affect the immutable {ManagedEntity}} in both PersistenceContexts. Has this use case been considered? |