Lazy lookup of a dictionary value using a stateless session

≯℡__Kan透↙ 提交于 2019-12-10 20:39:46

问题


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]?


回答1:


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?




回答2:


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.



来源:https://stackoverflow.com/questions/4777278/lazy-lookup-of-a-dictionary-value-using-a-stateless-session

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!