Your assumption (about first level caching) is correct. As for example stated here: Interface Session:
The main runtime interface between a Java application and Hibernate.
This is the central API class abstracting the notion of a persistence service.
Or here Chapter 2. Architecture; 2.1. Overview
Extract: Session (org.hibernate.Session)
A single-threaded, short-lived object representing a conversation between the application and the persistent store. It wraps a JDBC
connection and is a factory for Transaction. Session holds a mandatory
first-level cache of persistent objects that are used when navigating
the object graph or looking up objects by identifier.
And also, you can see the methods available for us, to remove an object form the session:
And many more. Evict
in this case should be working. We have to take the current instance ('A') and explicitly Evict
it from the session.
If we've already loaded some/more stuff, and we do not know, what to Evict()
, we simply need to get the fresh data. Then we can call Clear()
to completely reset the session and start again.
This is a bit radical, because none of the objects in the session will be updated/inserted on session Flush()
... but it could be what we want in this scenario (very often used for testing... load, clear... change and flush)