Hibernate stores cached entities as instances of CachedEntry class which means that every time an entity is requested from the second level cache, a new instance of that entity is created from the data contained in CachedEntry.
In most cases, this is a good approach because it prevents the cached data from being accidentally modified by code which requests the entities. But when the stored entities are immutable, this cannot happen.
We have a bunch of entity objects which contain quite large data (have a lot of attributes), and once written, they can never change, hence the entity objects are immutable.
Is there a way to force Hibernate to store immutable objects as "actual instances" meaning that for each distinct entity, only one immutable instance would exist in the cache which would always be returned, instead of creating new instances all the time?
I would expect Hibernate to have this behavior with objects annotated with @Immutable, but that's not the case.