File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Object Relational Mapping and the fly likes 2nd Level cache eviction not cascading Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "2nd Level cache eviction not cascading" Watch "2nd Level cache eviction not cascading" New topic
Author

2nd Level cache eviction not cascading

Jonathan Huang
Ranch Hand

Joined: Jun 23, 2006
Posts: 35
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.

Hopefully someone here could assist me!
http://forum.hibernate.org/viewtopic.php?t=962939

Thanks in advance. I'd really appreciate any guidance.
Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 9317
    
109

Use the to evict the collection.
The collections are cached separately from the parent objects.


[My Blog] [JavaRanch Journal]
Jonathan Huang
Ranch Hand

Joined: Jun 23, 2006
Posts: 35
Okay, so if I have these simple mappings:



This creates 3 regions in my second level cache:
Parent
Parent.myChildren
Child

If we don't cache the Child mapping, hibernate won't associate the Child with parent when loading Parent. It does:

collection initialized
initializing collection Parent.myChildren#1
checking second-level cache - Cache hit: Parent.myChildren#1
loading entity: Child#component[ID]{ChildId=1, ParentId=1}]
attempting to resolve: Child#component[ID]{ChildId=1, ParentId=1}]
object not resolved in any cache: Child#component[ID]{ChildId=1, ParentId=1}]

Then it goes and hits the DB, which we don't want.

Now, when I place cascade=evict on the myChildren set in Parent, shouldn't the evict cascade to Child.

So that Parent, Parent.myChildren, and Child are all evicted from the second level cache? If not, then how do I get eviction to cascade to all the collections of a parent object?

[ August 08, 2006: Message edited by: Jonathan Huang ]
[ August 08, 2006: Message edited by: Jonathan Huang ]
Jonathan Huang
Ranch Hand

Joined: Jun 23, 2006
Posts: 35
Does anyone know if this is possible?

I've been looking through the source, and it seems only the session level cache cascades through the children. The second level cache doesn't. Maybe I am wrong and missed something. . .
Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 9317
    
109

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.
Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 9317
    
109

Here's the code that you will have to use for this particular example that you have:



You will be passing the rolename(Rolename=ParentClassName + "." + NameGivenToTheCollectionMapping) and the id of the Parent class, whose collection you are evicting
Jonathan Huang
Ranch Hand

Joined: Jun 23, 2006
Posts: 35
I am doing exactly that, but it is not evicting Child. It evicts Parent and MyChildren but does NOT evict Child.

That's why I am so confused. From my understanding it is suppose to evict Child as well.
[ August 17, 2006: Message edited by: Jonathan Huang ]
Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 9317
    
109

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
Jonathan Huang
Ranch Hand

Joined: Jun 23, 2006
Posts: 35
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 ]
Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 9317
    
109

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)
Jonathan Huang
Ranch Hand

Joined: Jun 23, 2006
Posts: 35
Im looping through the cache stats (in a .jsp page):
This is how I know what exactly is being evicted and what is not.



And just so we're both clear, The Parent.Child collection is being evicted from the Second Level Cache.

The Child objects that Parent.Child points to are NOT being evicted. That's where im having issues.
[ August 18, 2006: Message edited by: Jonathan Huang ]
Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 9317
    
109

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.
Jonathan Huang
Ranch Hand

Joined: Jun 23, 2006
Posts: 35
Well, at least I know I am not going crazy.

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: 2nd Level cache eviction not cascading
 
Similar Threads
Automatic Optimistic Concurrency
select using hibernate - TIME dataype
c3p0(JDBC DataSources/Resource Pools) Vs Apache Commons DBCP
org.hibernate.Criteria: field ROOT_ENTITY not found
collection with cascade="all-delete-orphan" was no longer...