wood burning stoves 2.0*
The moose likes Object Relational Mapping and the fly likes Hibernate Caching not clear Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Hibernate Caching not clear" Watch "Hibernate Caching not clear" New topic
Author

Hibernate Caching not clear

Poornima Sharma
Ranch Hand

Joined: Sep 09, 2008
Posts: 114
Hi Friends,

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?

Please guide.

Thanks,


Poornima Sharma
SCJP 6.0
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

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).


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Poornima Sharma
Ranch Hand

Joined: Sep 09, 2008
Posts: 114
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?

Please guide.
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

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.
Poornima Sharma
Ranch Hand

Joined: Sep 09, 2008
Posts: 114
Thanks a lot. It means that my master table will always fetch the collection data from the database if I have not explicitly cached the collection.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Hibernate Caching not clear
 
Similar Threads
Hibernate mapping issue
Id key in .hbm.xml
Create table without any key in Hibernate
1-to-M mapping confusion
problem with hibernate mapping