I was wondering what would happen, if I change a property of a DOM object retrieved from Hibernates second level cache. I found this quote:
2nd level cache stores a map of entities' IDs to other properties (it doesn't actually store objects, but the data itself)
in this answer: https://stackoverflow.com/a/7059822/2468620.
This should mean that when I retrieve an entity from second level cache, it is "freshly" created from the stored raw data. Therefore if I change properties of this entity, it shouldn't affect the data stored in second level cache. That meaning when I retrieve the entity again in another session, its properties should not be affected by the described change.
For examle:
user = userDao.find(1); // get user stored in second level cache
System.out.println(user.getName()); // returns "originalName"
user.setName("modifiedName");
// === this is done in a different session ===
user = userDao.find(1);
System.out.println(user.getName()); // should return "originalName"
I tried the above example with eh-cahce and it really works but my question is:
Is this behavior guaranteed? I mean if I can depend on it even when I change the cache provider, or when the implementation of my used cache provider changes? I looked in the JPA specification and didn't find information on this.