I have a one-to-many relationship between Parent and Child tables. I have read-write caching (EHCache) and query caching enabled, and I have defined caching in both Parent.hbm.xml and Child.hbm.xml as well as the Child set in Parent.hbm.xml.
Running queries works great and by following the output from show_sql I can see that the data gets cached ok and table read are eliminated.
The problem I am having is when I create a new Child. First I load an existing Parent object. Next I create a new Child object instance and set the Person object using the Child's setParent() method. Next I call HibernateUtil.saveOrUpdate() to save the Child.
Once this is done I can query the database and see that all of the data was saved ok. But when I re-load that Parent using hibernate, the change is not visible. So I cannot see the Child attached to the Parent.
Originally posted by Andy Hahn: I dont think you need to define for each region, that is optional according to the ehcache documentation.
OK, if the docs say they are optional, then they are optional.
But really I guess then back to the original question.
The question for me now resides with are you still in the same Session when you requery for the parent? And if so, that means the cached parent object you get back might be from either the first level cache or the second level cache. If it is in the first level cache, I recall some sort of refresh(Object) type method in the Session that will tell Hibernate to ignore any instances in the Persistence Context, and go back to the database. Although if you added the child in the same session with the Parent object that is in the Persistence Context, then when you get it again from the Persistence Context, it should be the same with that new child. hmm.
If not in the same session, then it is prbably from the second level cache. So if it is the second level cache, I would ask what is the "stale" setting for that cache region, or the "stale" setting for all regions (What you would get not defining each region). When I say stale, I mean the setting that says when to refresh the cache, is it at every query, at a certain time period, or after the cache reaches a certain size.
But I will say this, most issues are in the configuration files.
I'm not in the same session. So they are detached. I think the problem has to do with the way hibernate caches in the 2nd level cache. I couldnt get a real solution and gave up on debugging the hibernate source real fast, whoa. So as a workaround, when I need to make the parent-child updates I just call evict() and evictCollection() on the objects and their relationships. I bet I could have got this to work if I had not set parent on child and instead added child to the parent set and then saved parent. Thanks for looking into. Andy
He does not suffer fools gladly. But this tiny ad does:
Devious Experiments for a Truly Passive Greenhouse!