Im cross referencing a post I made in the Official Hibernate Forums.
Essentially, I can't get my second level cache to cascade evictions. The parent object has a number of bags, that are all cached on load. (I set all the bags to be cachable as well as cascade="evict,all, all-delete-orphan, etc. . . ."). I can evict the parent but the associated collections won't evict as well.
As i already mentioned in my previous post, you will have to explicitly evict the collection from the second level cache. This question is similar to the one answered here in the Hibernate FAQ. In short, the collection cache is maintained separately and you will have to explicitly clear it. If you are looking for the exact code to do the same, let us know, i will try and dig it out from some of the examples that i have.
From my understanding it is suppose to evict Child as well.
If i have correctly understood the working of hibernate cache, then i dont think what you are expecting is correct. Parent and Child are two separate entities(even though you maintain a collection of the Child objects in your Parent). If i get you right, you are expecting the objects of type Child to be evicted when you are evicting a object of type Parent. This is wrong, consider the following case: - You have a Parent with id = 500 which is *associated* with a Child with id = 200 - Let's consider that there is one more Child(no where related to Parent with id = 500) with id = 300 - Assume all these objects are second level cached.
Now, when you invoke evict(Parent.class, 500), you are expecting to evict Parent with id = 500, Child with id = 200 and Child with id = 300. This is wrong since Child with id = 300 is no where related to Parenat with id = 500.
What you would ideally, want in the above case is to evict Parent with id = 500 and Child with id = 200. This can be achieved through the code mentioned in my earlier post.
Let us know, if you see any issues with this or if you think i havent got your question right
Sorry, let me try to explain myself a little bit better. This is essentailly my expectations:
A1 has a collection B1 A2 has a Collection B2
B1 contains B_B1, B_B2, B_B3 B2 contains B_B5
B_B1 Points to Q1, B_B2->Q2 and B_B3->Q3. B_B2->Q10 If I evict A1 (and it's collections), shouldn't A1, B1, and Q1, Q2, Q3 all be Evicted from the cache. So, not the entire cache region, but just the associated values.
So for a more concrete example (all set to be cachable):
Parent.id=1 that has a collection of child.id=1, child.id=2, child.id=3.
Parent.id=2 that has a collection of child.id=2, child.id=4, child.id.5
In hibernate's cache stats, I will see 3 regions: Parent, Parent.Child, and Child.
In the above example, Parent.id=1 and Parent.id=2 share Child.id=2.
There are 2 things which I don't understand going on:
1) If you set Cascade=evict on the Parent.Child collection in the mapping file, why does: SessionFactory.evict(Parent.id=1), not evict Parent.Child where Parent.id=1
2) If you do SessionFactory.evictCollection(Parent.child, id=parent.id=1) why does: Parent.Child.id=1, parent.Child.id=2, and Parent.Child.id=3 (the parent's collection) is evicted from the cache. But why isn't Child.id=1, Child.id=2, and Child.id=3 NOT evicted.
Still left in the cache SHOULD be: Parent.id=2 Parent.Child.id=4, Parent.Child.id=5 Child.id=4, Child.id=5
It is my understanding that if cascade=evict (or as the documentation states, should be set to "all" or "all-delete-orphan"), on the parent's collections, the associated collections will be evicted. On top of that, since the associated collections, are really pointers to another entity in the cache, those should be evicted as well. Not the entire cache region, but just the ones associated.
Hope that helps explain myself better... [ August 18, 2006: Message edited by: Jonathan Huang ]
If you do SessionFactory.evictCollection(Parent.child, id=parent.id=1) why isn't Child.id=1, Child.id=2, and Child.id=3 NOT evicted.
These child objects should have been evicted. How are you checking whether the objects have been evicted from the second level cache or not? In the meantime i will try out a small example of this scenario - might take some time, today being a weekend(I do believe it should work)
I tried out the scenario, you have mentioned here and indeed the Child objects are not evicted from cache. I even tried it without using the Statitics util of Hibernate, just to make sure that there was nothing wrong with that util. The Child objects continue to remain in the cache.
I've been working through the second level cache functionality, returning the CacheTypes and playing around with them. Im trying to find an association between the Role Name, and the Entity Name so I can evict them myself progamatically. The best I can do so far is evict the entire region, and not the individual associated values.
I'll let you know how it goes.
You don't like waffles? Well, do you like this tiny ad?