使用无状态会话延迟查找字典值

发布于 2024-10-14 00:46:58 字数 924 浏览 7 评论 0原文

在我的应用程序中,我设置了一个三元字典映射,以便对于给定用户,我可以检索属于该用户的对象的每个实例的“设置”。也就是说,我有这样的东西:

public class User
{
    public virtual IDictionary<Baz, BazSettings> BazSettings { get; set; }

    //...

所以只要我有一个 Baz 对象,我就可以通过 currentUser.BazSettings[baz] 查找当前用户的 baz 设置。

我希望能够使用无状态会话来执行此操作,但我收到以下代码的 LazyInitializationException

//int bazId;
using (IStatelessSession session = Global.SessionFactory.OpenStatelessSession())
{
    var currentUser = session.Get<User>(Membership.GetUser().ProviderUserKey);
    var baz = session.Get<Baz>(bazId);
    var bazSettings = currentUser.BazSettings[baz]; // raises `LazyInitializationException`

当我使用 ISession 时,问题就消失了。

完整的 NHibernate 错误消息包括文本“没有会话或会话已关闭”。这是有道理的,因为使用无状态会话时,实体不会连接到会话。但是,我认为有一种方法可以使用无状态会话来执行此查找。

如何使用无状态会话来执行查找 currentUser.BazSettings[baz]

In my app, I set up a ternary dictionary mapping so that for a given user, I can retrieve "settings" for each instance of an object that belongs to the user. That is, I have something like:

public class User
{
    public virtual IDictionary<Baz, BazSettings> BazSettings { get; set; }

    //...

So whenever I have a Baz object, I can lookup the current user's baz settings via currentUser.BazSettings[baz].

I would like to be able to use a stateless session to do this, but I get a LazyInitializationException with this code:

//int bazId;
using (IStatelessSession session = Global.SessionFactory.OpenStatelessSession())
{
    var currentUser = session.Get<User>(Membership.GetUser().ProviderUserKey);
    var baz = session.Get<Baz>(bazId);
    var bazSettings = currentUser.BazSettings[baz]; // raises `LazyInitializationException`

When I use instead an ISession, then the problem goes away.

The full NHibernate error message includes the text "no session or session was closed". This makes sense because when using a stateless session, entities are not connected to the session. However, I would think that there is a way to use the stateless session to perform this lookup.

How do I use a stateless session to perform the lookup currentUser.BazSettings[baz]?

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

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

发布评论

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

评论(2

因为看清所以看轻 2024-10-21 00:46:58

无状态会话不支持延迟加载,正是因为它们是无状态:它们不跟踪有关使用它们检索的实体的任何内容

使其工作的唯一方法是立即加载集合。但是,如果无状态会话显然不能提供您所需要的内容,为什么您想要使用无状态会话呢?

Stateless sessions do not support lazy loading, exactly because they are stateless: they do not track anything about the entities retrieved with them.

The only way to make it work is eager loading the collection. But why do you want to use stateless sessions, if they clearly do not provide what you need?

水水月牙 2024-10-21 00:46:58

您应该使用 ISession 而不是 IStatelessSession,因为使用无状态会话执行的操作不会级联到关联的实例,并且无状态会话会忽略集合。

You should use ISession instead of IStatelessSession because operations performed using a stateless session do not cascade to associated instances and collections are ignored by a stateless session.

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