Hibernate stores entities in a dehydrated form in its 2nd level cache. This works fine for me until custom user types enter the scene, particulary <a href="http://jadira.sourceforge.net/usertype-userguide.html" rel="nofollow">Jadira's UserType</a> library, which I use to map Joda time types.
Persistence works fine out of the box, but <a href="https://stackoverflow.com/questions/25869959/ehcache-hibernate-2nd-level-cache-maxbyteslocalheap-slow/26010141#26010141" rel="nofollow">I've discovered</a> that Hibernate will put a "raw" (serialized) reference to a
LocalDate instance into its 2nd level cache, which causes some problems, because each
LocalDate instance keeps tons of references to internal datastructures, which should not turn up in the cache.
Is there a way to hook into Hibernate's (de-)hydration strategy to get fine grain control over which data will eventually be stored in it's 2nd level cache?Answer1:
Jadira's UserType library uses
org.hibernate.usertype.UserType to implement its custom types.
UserType instances are simply serialized when put into the 2nd level cache, which is a less then ideal choice for Joda time instances.
The Problem could be solved by using
org.hibernate.usertype.CompositeUserType instead when implementing custom types, which gives you full control how entities should be (de-)hydrated when put into (or taken out of) the cache.
I filed a feature request with Jadira, but for now I'll just move forward by implementing my own custom types.