Nhibernate - 未能懒惰地初始化集合...没有会话或会话被关闭

发布于 2024-10-25 11:38:44 字数 2036 浏览 3 评论 0原文

我收到此错误,想知道是否有人知道如何调试此错误。

谢谢

初始化[GL.Objects.Profile`1[[GL.Objects.Education.Education, GL.对象,版本=0.1.1.0, 文化=中立, PublicKeyToken=ebf25c7be4be0c91]]#289289]

-未能延迟初始化角色集合:

GL.Objects.Profile`1[[GL.Objects.Education.Education, GL.对象,版本=0.1.1.0, 文化=中立, PublicKeyToken=ebf25c7be4be0c91]].Profileables,

没有会话或会话已关闭”

启动会话

        var watsonService = new WatsonService();

此代码保存实例化的对象。

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

第一个断言通过得很好。 epf2 有一个 id 并被实例化。

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

此断言失败。 Profileables 与 id 已成功插入的复合表相匹配。但现在,当我尝试访问集合中的第一个元素时,它失败了。

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

这是 GetEducationProfile 方法调用的方法实现。

public T Get<T>(int id) where T : IDataObject
{
    return (T)_session.Load(typeof(T), id, LockMode.Read);
}

这是 Fluent 映射文件中构造函数的定义。

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");
    }

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.

public 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");
    }

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

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

发布评论

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

评论(1

帅气尐潴 2024-11-01 11:38:44

对于没有提供与该问题相关的更多信息,我深表歉意。
问题来自于会话的管理方式。

我的测试正在实例化以下类:

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

SaveEducation 方法调用以下方法

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

这将打开一个会话。

返回时,会话将被丢弃,因此不会发生延迟加载。

我需要做的是确保在会话仍然打开时发生延迟加载。

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.

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