休眠:清理集合的二级缓存,同时级联删除项目 [英] Hibernate: Clean collection's 2nd level cache while cascade delete items

查看:124
本文介绍了休眠:清理集合的二级缓存,同时级联删除项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题Hibernate没有更新二级缓存来处理级联删除项目的集合。



详细信息



假设我们有一个有Parent.myChildren集合的Child对象的对象Parent。
现在我们还用Humans.myAllHumans集合来反对Humans,并且所有Parent和Child对象都在这个集合中。

现在我们 .delete(parent)和所有孩子都会从数据库中级联移除,但Humans.myAllHumans集合的缓存不会更新!它仍然假定级联删除的对象在数据库中,我们在尝试访问集合时碰到以下异常:

org.hibernate.ObjectNotFoundException:没有具有给定标识符的行:[foo .Child#751]

接近尝试



1) SessionFactory.evictCollection()方法,但据我所知它不是事务安全的,并且很难从二级缓存中删除数据,我不希望这样。



2)我也可以手动(编程)从myAllHumans集合中删除每个对象。在这种情况下,hibernate会更新二级缓存。这种方法我想避免,因为它只是使级联删除功能无用。



预计



I' d像hibernate一样聪明,足以自动更新集合的缓存。可能吗?

我现在在使用EhCache,你认为使用另一个缓存实现或者配置EhCache可能有帮助吗?

解决方案

我一直在努力解决其他需要从缓存中删除集合的问题,并且我已经找到了一些解决方案。我不知道是否可以在级联删除时自动更新集合的缓存,但是如果您尝试过SessionFactory.evictCollection()并且它工作正常,我认为这个解决方案可以是事务安全的并且它也可以工作:
((AbstractPersistentCollection)MYCOLLECTION).dirty ();


I have a problem Hibernate does not update 2nd level cache for a collection of items which are subject of cascade removal.

Details

Assume 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 tried

1) 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.

Expected

I'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'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();

这篇关于休眠:清理集合的二级缓存,同时级联删除项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆