Nhibernate 不会使缓存集合失效
我有两个对象 - ContentPage
,其中包含 ChildLinks
的集合。
ContentPage
-----------
ID
Title
ChildLink
----------
ID
ParentPageID [ContentPage]
ChildPageID [ContentPage]
Priority
ContentPage.ChildLinks
属性利用二级缓存。我正在使用 Fluent NH 配置 Nhibernate,并使用 Nhibernate 3.1。集合和“ChildLink”类的缓存均设置为“读写”。
我注意到,每当我删除 ChildLink
时,集合缓存都不会失效。因此,当我调用 ContentPage.ChildLinks
时,我收到错误:
no row with the given identifier exists
我已关闭缓存,并且它运行良好。缓存不是应该自动失效吗?我使用 SysCache 作为缓存提供程序,使用 MySQL 作为数据库。
提前致谢!
I have two objects - ContentPage
, which has a collection of ChildLinks
.
ContentPage
-----------
ID
Title
ChildLink
----------
ID
ParentPageID [ContentPage]
ChildPageID [ContentPage]
Priority
The ContentPage.ChildLinks
property utilises the 2nd level cache. I am using Fluent NH to configure Nhibernate, and using Nhibernate 3.1. Cache is set as 'Read-Write' both for the collection, and the 'ChildLink' class.
I've noticed that whenever I delete a ChildLink
, the collection cache is not being invalidated. Thus, when I call the ContentPage.ChildLinks
, I get an error:
no row with the given identifier exists
I've turned off the cache, and it works well. Shouldn't the cache be automatically invalidated? I am using SysCache as the cache provider, and MySQL as the database.
Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我遇到了同样的问题,我可以通过以下文章解决我的问题:
反向映射集合和 NHibernate 的二级缓存
基本上,如果您将集合映射为反向,当您删除子项时,您还必须确保显式地将其从父集合中删除,否则删除子项后缓存状态将无效。首先要检查的是这种关系是否真的需要是反向的。
假设逆是必要或期望的,并使用您的示例:
而不只是像这样:
您必须这样做:
此时您可能还需要显式保存您的 ContentPage 对象,这取决于您的会话刷新设置。
我在实体上使用方法来管理此类反向关系,例如:
然后在删除子对象时:
I had the same problem and I can across the following article which solved my problem:
Inverse Mapped Collections and NHibernate's Second-Level Cache
Basically if you have mapped your collections as inverse, when you delete the child item you also have to make sure to explicitly remove it from the parent collection or the cache state will be invalid after you delete the child. The first thing to check is if the relationship really needs to be inverse.
Assuming inverse is necessary or desired, and using your example:
Instead of only something like:
You have to do:
You also might need to explicitly save your ContentPage object at this point as well, it depends on your Session flush settings.
I use methods on my entities for managing such inverse relationships, for example:
And then when deleting a child object: