为什么NHibernate延迟加载绑定到session?

发布于 2024-08-07 04:25:26 字数 770 浏览 4 评论 0原文

使用 Castle ActiveRecord,我在延迟加载时遇到了一个问题。

以下工作(显然)

using (new SessionScope())
{
    User singleUser = User.FindFirst(...)
    UserGroup groups = singleUser.Groups; // Lazy-loading groups.
}

由于我需要在特定上下文中修改会话过滤器(使用拦截器),因此我创建了一个新的 SessionScope。

using (new SessionScope())
{
    User singleUser;
    EnableVariousFiltersInThisThread();
    using (new SessionScope())
    {
        singleUser = User.FindFirst(...);
    }
    DisableVariousFiltersInThisThread();
    UserGroup groups = singleUser.Groups; // Lazy-loading groups.
}

最后一行“singleUser.Groups”抛出 LazyInitializationException:“无法延迟初始化角色集合:组,没有会话或会话已关闭”。

但是,所有其他会话操作都可以正常工作。因此,“singleUser”似乎绑定到现在已处置的 SessionScope。为什么?或者如何解决这个问题?

Using Castle ActiveRecord, I stumbled into a problem when lazy-loading.

The following works (obviously)

using (new SessionScope())
{
    User singleUser = User.FindFirst(...)
    UserGroup groups = singleUser.Groups; // Lazy-loading groups.
}

Since I need to modify the session filters in a certain context (using interceptors), I create a new SessionScope.

using (new SessionScope())
{
    User singleUser;
    EnableVariousFiltersInThisThread();
    using (new SessionScope())
    {
        singleUser = User.FindFirst(...);
    }
    DisableVariousFiltersInThisThread();
    UserGroup groups = singleUser.Groups; // Lazy-loading groups.
}

The last line "singleUser.Groups" throws a LazyInitializationException: "failed to lazily initialize a collection of role: Groups, no session or session was closed".

However, all other session operations work correctly. So it seems that "singleUser" is bound to the now disposed SessionScope. Why? And how can this be solved alternatively?

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

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

发布评论

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

评论(2

甜嗑 2024-08-14 04:25:26

我相信这就是 NHibernate 的工作方式。

您的实体都与会话相关联,并使用它来延迟加载。如果您处置会话,则无法获取延迟加载的集合和属性。考虑到这一限制,显而易见的答案是避免处置会话,或者保持会话处于活动状态,直到提取所需的数据。

内部范围与外部范围没有什么不同;它也不支持在此范围之外的延迟加载。

但是,如果您欺骗 NHibernate 在您的内部范围内进行急切加载,则可以解决此限制。或者,在退出范围之前,对要使用的集合调用 .ToList() 或类似方法,数据也将在外部可用。

I believe this is the way NHibernate works.

Your entities are all associated with a session and use this for lazy loading. If you dispose your session, you can't fetch lazy loaded collections and properties. The answer, obvious considering this limitation, is to avoid disposing the session - or to keep the session alive until you have extracted the data you need.

The inner scope is no different from the outer; it doesn't support lazy loading outside this scope either.

You can however work around this limitation, if you trick NHibernate into eager loading within your inner scope. Alternatively, call .ToList() or similar on the collections you would want to work with, before exiting the scope and the data will also be available outside.

分開簡單 2024-08-14 04:25:26

我的猜测是 - 部分原因与“身份地图”有关。不仅是延迟加载对象,而且所有对象都绑定到会话。这可以确保没有两个对象代表数据库中的一行。

My GUESS is - part of the reason is about "Identity Map". Not only lazy loaded objects, but also all objects are bound to a session. This makes sure no two objects represent a single row in database.

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