Based on the documentation it seems one can only configure the second level cache to cache every instance of a class. Meaning the configuration is only at the class level.
Is it possible to specify that an entity be cached only if it matches some Criteria?
My use case is along the lines of having lots of incoming data, with each piece of incoming data requiring we select some state out of the DB to see if 1. the state is 'bad', 2. if the state would be made 'good' by processing the incoming data. I would want to only cache data for the entities where the state is currently 'bad'. Most of the time(95% or more), the data would not change the state, and the number of entities in a bad state are small. But constantly selecting state out of the DB per incoming piece of data to see if some bad state could be potentially cleared doesn't scale well.
I could of course manually manage a cache, but I'd have to hook into some Hibernate events to see if the transaction failed and rollback changes to the cache, or commit the cache, etc. Which are all problems already solved in general by Hibernate transaction + cache guarantees. Except that I don't see any way to be more specific to again cache only based on Criteria.
Is there anything existing that I can leverage here, or is the current extent of the functionality to only specify caching at the class level?
|