开发者

Nhibernate - failed to laizly initialize a collection ... no session or session was closed

开发者 https://www.devze.com 2023-02-18 05:49 出处:网络
I am getting this error, and am wondering if anyone has any idea how to debug this. Thanks Initializing[GL.Objects.Profile`1[[GL.Objects.Education.Education,

I am getting this error, and am wondering if anyone has any idea how to debug this.

Thanks

Initializing[GL.Objects.Profile`1[[GL.Objects.Education.Education, GL.Objects, Version=0.1.1.0, Culture=neutral, PublicKeyToken=ebf25c7be4be0c91]]#289289]

-failed to lazily initialize a collection of role:

GL.Objects.Profile`1[[GL.Objects.Education.Education, GL.Objects, Version=0.1.1.0, Culture=neutral, PublicKeyToken=ebf25c7be4be0c91]].Profilables,

no session or session was closed"

Starts up the session

        var watsonService = new WatsonService();

This code saves the instantiated objects.

   watsonService.SaveEducation(e);
    epf1.Profilables.Add(e);
    watsonService.SaveEducationProfile(epf1);
    epf2 = watsonService.GetEducationProfile(epf1.ID.Value);

The first assert passes just fine. epf2 has an id and is instantiated.

        Assert.AreEqual(epf1.ID, epf2.ID);

This assert fails. The Profileables matches a composite table which id's get inserted successfully. But now when I try to access the first element in the collection it fails.

        Assert.AreEqual(epf1.Profilables[0].ID, epf2.Profilables[0].ID);

This is the method implementation that get's called by the GetEducationProfile method.

publi开发者_如何学Goc T Get<T>(int id) where T : IDataObject
{
    return (T)_session.Load(typeof(T), id, LockMode.Read);
}

This is the definition of the constructor in the Fluent Mapping file.

public EducationProfileMap()
    {
        Table("Profile");
        Id(x => x.ID)
        .Column("ProfileID")
        .GeneratedBy
        .HiLo(FluentConst.HILO_TABLE,
              FluentConst.NEXTHI_COLUMN,
              FluentConst.MAXLO, 
              String.Format(FluentConst.WHERE_FMT_STR, "Profile"));

        Map(x => x.Type).CustomType<int>().Column("ProfileType");

        HasManyToMany(x => x.Profilables)
                    .ParentKeyColumn("ProfileID")
                    .ChildKeyColumn("EducationID")
                    .Cascade.All()
                    .Table("EducationProfile");
    }


My apologies for not giving more relevant information to the problem. The Problem comes from how the sessions are managed.

My test is instantiating the following class :

    public WatsonAdaptor(string user)
    {
        if (DataSession == null)
         DataSession = new  HibernateSession(
                       HibernateFactoryManager.HibernateFactory.Watson, user);
    }

The following method is called by the SaveEducation method

   public int? SaveEducation(Education e) {
        try
        {
            var watson = new WatsonAdaptor("ealite");
            watson.Save(e);
            return e.ID;
        }
        catch (Exception ex)

Which opens a session.

On the return the session is thrown away so lazy loading can't happen.

What I need to do, is to make sure the lazy loading is happening while the session is still open.

0

精彩评论

暂无评论...
验证码 换一张
取 消