DetailsAssume we have an object Parent which has Parent.myChildren collection of Child objects. Now we have also object Humans with Humans.myAllHumans collection and all Parent and Child objects are in that collection.
Now we session.delete(parent) and all the children are cascade removed from the database, but Humans.myAllHumans collection's cache is not updated! It still assumes that cascade deleted objects are in database and we hit following exception while trying to iterrate the collection later:
org.hibernate.ObjectNotFoundException: No row with the given identifier exists: [foo.Child#751]
Approaches tried1) I've tried SessionFactory.evictCollection() approach, but as I understand it is not transaction safe and hard removes data from 2nd level cache, I do not want that. 2) I can also manually (programatically) remove each object from the myAllHumans collection. In this case hibernate does update 2nd level cache. This approach I'll like to avoid since it just makes cascade delete feature useless.
ExpectedI'd like hibernate to be smart enough to update the collection's cache automatically. Is it possible?
I'm using EhCache now, do you think using another cache implementation or configuring EhCache may help?
I guess my problem case can be simplified. Assume I call just "session.delete(child)", how can I be sure Parent.myChildren 2nd level cache is synchronised?