简单的问题,从父级检索更新的 nhibernate 对象集合

发布于 2024-11-17 08:44:39 字数 949 浏览 0 评论 0原文

我有一个关于 nHibernate 的非常简单的问题,但我在谷歌上找不到答案。

假设我有一个映射的 Member 对象,它包含 Mail 对象的 1:n 外键。我想在用户进入收件箱时检索其当前的邮件项目集合。如果我调用 memberInstance.Mail (类型为 IList<\Mail>),它会返回邮件集合,但不会反映添加的新邮件,因为 memberInstance 在登录期间保存在缓存中,并且不往返数据库。如果我创建一个 RetrieveMailForUser 方法,向其传递一个 Member 实例,然后调用诸如 Session.CreateCriteria 之类的代码,我会获取任何给定成员的当前邮件以及除如果我从不使用成员变量上的邮件集合,并且在成员变量中拥有不是最新的集合,那么在成员变量上拥有邮件集合似乎是无关紧要的。我可以在每次用户检查收件箱时刷新成员并将其保存到缓存中,但这似乎有很多开销。我觉得我缺少 nHibernate 的一些基本原理。请帮忙。谢谢!

调用成员的代码:

      public static Member GetLoggedInUser()
        {
             var member = (Member) HttpContext.Current.Session[SESSION_NAME];

            if(member != null && 
!NHibernateSessionManager.IsEntityAttachedToCurrentSession(member))
                NHibernateSessionManager.AttachEntityToSession(member);
            return member;


        }

E

I have a pretty easy question regarding nHibernate but I couldn't really find an answer on google.

Let's say I have a mapped Member object that holds a 1:n foreign key to Mail objects. I would like to retrieve the current collection of mail items for a user when he enters his inbox. If I call memberInstance.Mail (of type IList<\Mail>) it brings back the mail collection but doesn't reflect the new mail that was added since the memberInstance is saved in the cache during login and doesn't make a round trip to the database. If I create a RetrieveMailForUser method, pass it a Member instance and then call code such as Session.CreateCriteria<Mail> I get the current mail for any given member and everything but it seems extraneous to have a Mail collection on the Member variable if I never use it as well as having a collection in the member var that isn't up to date. I could refresh the member every time the user checks his inbox and save it to the cache but this seems to have a lot of overhead. I feel like I'm missing some basic principal of nHibernate. Please help. Thanks!

Code that calls the member:

      public static Member GetLoggedInUser()
        {
             var member = (Member) HttpContext.Current.Session[SESSION_NAME];

            if(member != null && 
!NHibernateSessionManager.IsEntityAttachedToCurrentSession(member))
                NHibernateSessionManager.AttachEntityToSession(member);
            return member;


        }

E

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

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

发布评论

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

评论(1

っ左 2024-11-24 08:44:39

从您的帖子中,我得出两个相互矛盾的要求:

  1. 您希望缓存中有一个对象(成员),该对象不应根据数据库中发生的情况进行更新。缓存的本质是不会自动重新加载任何内容或尝试这样做。

  2. 您希望每次访问 Member 对象的 Mails 集合中的邮件时都进行更新(因此忽略第一次检索 Member 对象时缓存中的内容)。

如果您想坚持使用第一种方法,则必须在为该对象创建新邮件时直接更新缓存中的 Member 对象(调用 member.AddMail(mail) 或类似的方法)。我不知道这是否可行,因为您显然将 Member 保留在会话中,并且这需要访问另一个会话的 Member 对象。

如果您还想要“最新”的邮件列表,我想没有办法显式地重新加载您的邮件列表,重新加载整个对象,或者只是按照您上面建议的方式加载邮件(RetrieveMailForUser) 。在这种情况下,您可以放心地删除 Member 对象中的 Mails 集合。如果不需要,则不必映射集合。

from your post I derive two contradicting requirements:

  1. You want an object (Member) in the cache, which should not be updated according to what happens in the database. It is in the very nature of a cache NOT to automatically reload anything or trying to attempt to do so.

  2. You want Mails in the Mails collection of the Member object to be updated whenever accessed (and therefore disregarding what was in the cache at the moment of retrieving the Member object for the first time).

If you want to stick to the first approach, you must update the Member object in the cache directly whenever a new Mail is created for that object (calling member.AddMail(mail) or something like that). I don't know whether that is feasible, since you apparently keep Member in the session, and that would require accessing Member objects of another session.

If you also want 'up to date' list of mails, I guess there is no way around either re-loading your mail list explicitly, reloading your entire object, or just loading the mails in the way you suggested it above already (RetrieveMailForUser). In that case, you can confidently remove the Mails collection in the Member object. You don't have to map collections if you don't need them.

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