Is there a way to obtain a list of "known" Entity instances for a given Session/EntityManager in JPA/Hibernate 4.x?
By "known" I mean either loaded, created or changed, i.e. a managed entity that exists in a persistence context. I know of EntityManager#contains
method so I'm guessing that such a list is maintained, but how can I get to it?
EDIT: Also, how can I query the state of the persistent entity (check if it is created, updated, deleted or clean in this persistence context)?
JPA does not define such a capability. But you can do it using Hibernate calls:
entityEntries
here is an array of Map.Entry instances whose "key" is the entity itself and whose value is an instance oforg.hibernate.engine.spi.EntityEntry
that describes various info about the entity, including information like EntityEntry.getStatus().