Found the following problem today:
Consider an entity E that has some relationship(s) to other lazily loaded entities (1:1, 1:n, or n:m - doesn't really matter for this issue). Assume we want to fetch two instances of E, one of them should load "more" data than the other, which is achieved by means of an entity graph:
There problem occurs for the special case when pk1 equals pk2; i.e., when the same entity is fetched twice, though we want to fetch "more" data for e2 (e.g., some of its relationships) than for e1. Using the environment mentioned by this issue, the entity graph for e2 is not considered in that case, seemingly because the object assigned to e2 is returned from the cache, as it has been fetched just before for e1.
It might be a corner case, but I think e2 should be assigned an object that has all the data mentioned by the entity graph specified loaded; whether it would be another instance (i.e. e1 == e2 is false) or the same instance (i.e. e1 == e2 is true) is an implementation detail. The latter seems preferable and could be achieved by dynamically setting the entity attributes mentioned in the entity graph of the second find in the cached entity instance and return it from the second find.
Wildfly 18.0.1, JDK 11.0.5