Hibernate: Clean collection's 2nd level cache while cascade delete items

吃可爱长大的小学妹 提交于 2019-12-03 06:59:14

The problem is that Hibernate doesn't actually do the delete. The database does that as part of a foreign key relationship, so Hibernate never sees all the objects that may get deleted and therefore, there is no way to update the cache that works in every case.

I think your best bet is to flush the cache (or part of it) when you delete.

I've been struggling with other problem requiring remove collection from cache and I've worked out some solution. I don't know if it is possible to update the collection's cache automatically on cascade delete, but if you've tried SessionFactory.evictCollection() and it worked, I think that this solution can be transactional safe and it works also:

if (MYCOLLECTION instanceof AbstractPersistentCollection) ((AbstractPersistentCollection) MYCOLLECTION).dirty();

Usually Hibernate need a politically incorrect refresh of the object to reload the cahe.

An important thing is how that EhCache handles the lazy properties. I found that the lazy attribute of the collection is not set, the cahe doesn't refresh the objects.

In your case, If the Humans set of the humanity attribute is set to lazy = true (default option), ehcache doesn't refresh it if the object. Try to set the lazy attribute of the humans and children collections to false.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!