Hibernate:在级联删除项目时清理集合的二级缓存

发布于 2024-08-05 21:09:07 字数 699 浏览 12 评论 0原文

我有一个问题,Hibernate 不会为级联删除的项目集合更新二级缓存。

详细信息

假设我们有一个 Parent 对象,其中包含 Child 对象的 Parent.myChildren 集合。 现在我们还有 Humans 对象和 Humans.myAllHumans 集合,并且所有 Parent 和 Child 对象都在该集合中。
现在我们session.delete(parent)和所有子级都从数据库中级联删除,但是Humans.myAllHumans集合的缓存没有更新!它仍然假设级联删除的对象位于数据库中,并且我们在稍后尝试迭代集合时遇到以下异常:
org.hibernate.ObjectNotFoundException:不存在具有给定标识符的行:[foo.Child#751]

尝试的方法

1)我尝试过SessionFactory.evictCollection()方法,但据我了解,它不是事务安全的,并且硬从二级缓存中删除数据,我不希望这样。

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

预计

我希望休眠足够智能来自动更新集合的缓存。可能吗?
我现在正在使用 EhCache,您认为使用其他缓存实现或配置 EhCache 可能有帮助吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

墨洒年华 2024-08-12 21:09:07

问题是 Hibernate 实际上并不执行删除操作。数据库将其作为外键关系的一部分来执行,因此 Hibernate 永远不会看到所有可能被删除的对象,因此无法更新在每种情况下都有效的缓存。

我认为最好的选择是在删除时刷新缓存(或其中的一部分)。

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.

〃安静 2024-08-12 21:09:07

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

if (MYCOLLECTION 抽象持久集合的实例)
((AbstractPersistentCollection) MYCOLLECTION).dirty();

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

离线来电— 2024-08-12 21:09:07

通常 Hibernate 需要对对象进行政治上不正确的刷新来重新加载缓存。

重要的是 EhCache 如何处理惰性属性。我发现集合的lazy属性没有设置,缓存没有刷新对象。

在您的情况下,如果 Humans 属性的 Humans 设置设置为lazy = true(默认选项),则 ehcache 不会刷新该对象。尝试将人类和儿童集合的惰性属性设置为 false。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文