I am using EHCache in hibernate and I have to implement readonly cache for all the master data tables. To implement it i made the following changes in hbm files:-
In my above code, the data of COMPANY Table is static, however the data in EMPLOYEE table gets updated, so I have to remove the second cache entry inside list from the above code. But im not sure, will i always fetch the updated data of EMPLOYEE when i fetch the data of COMPANY.
What is the process of caching? How does caching works with classes and collections?
If you have initialized the collection it will be in the cache, and you will have initialized it if you have accessed any property of the collection. Because this is a read only cache that data will not be refreshed in the cache if you update the employee from Hibernate. You could make the employee object not participate in the cache read-write (if that is appropriate).
Paul Sturrock wrote:You could make the employee object not participate in the cache read-write (if that is appropriate).
Yes, I don't want the employee object participate in the cache read or write. To implement this I have removed "<cache usage="read-only"/>" code from the list of Company hbm mapping file. Is that sufficient or do i need to remove the all the cache entires from the COMPANY hbm file?
Yes, that should be enough (assuming your entity is configured with the non-lazy cache attribute). Hibernate should now refresh the collection from the database when you access it in a new session, regardless of the state of the company record in the second level cache.