As far as I can tell, Hibernate is great at putting three kinds of objects into the second-level cache.
1. Mapped entities
2. Collections of the above that are mapped as one-to-many or many-to-many relations
3. Query results
But is it possible to cache a collection of mapped entities that results from some arbitrary operation?
I have a method:
Code:
public List obtainTheList(A ojbA) {
//Load up objects of Class B
//Do some logic to get objects of Class C
//Finally use these objects to get a list of Class D
return listOfD;
}
where classes A, B, C, D are all mapped entities. This operation to get listOfD from objA is expensive and would be ideal for caching. And it also doesn't lend itself easily to being expressed as a single query. In this example, the second-level cache is configured for classes A and D but not for B or C.
Does anyone know if it is possible to stuff the results into the second-level cache?
I'm using Ehcache as my cache provider, and I could certainly do this manually, but Hibernate does a lot of handy things when it puts a collection into the second-level cache. It would be time-consuming and error-prone for me to attempt to copy that mechanism.
For instance, it would be best to cache only the ids of listOfD and then pull the objects separately out of the cache for D.
I know Hibernate does this for mapped collections (#2 above). Is there a way to use it for collections that result from arbitrary operations?