【问题标题】:NHibernate LazyInitializationException when returning an Object返回对象时出现 NHibernate LazyInitializationException
【发布时间】:2012-08-24 11:23:25
【问题描述】:

有人可以向我解释为什么会发生这种情况 - 我在 Client 和 Project 之间有一个一对多的映射。

以下是各自映射文件中的两种关系:

客户:

<!-- Relationship with project -->
<bag name="projects" cascade ="all-delete-orphan" lazy="false">
  <key column="client_id" />
  <one-to-many class="Project" ></one-to-many>
</bag>

项目:

<many-to-one name="client"
         class="Client"
         column="client_id"
         cascade="all-delete-orphan" 
         fetch="join"
         not-null="false"
         lazy="false" />

以下是返回特定客户端的网络方法。

public Client RetrieveEqualsClient(string propertyName, object propertyValue)
{
    Client c = new Client();
    ConfigureNHibernate();
    using (ISession session = m_SessionFactory.OpenSession())
    {
        ICriteria criteria = session.CreateCriteria(typeof(Client));
        criteria.Add(Expression.Eq(propertyName, propertyValue));
        c = criteria.List<Client>()[0];

        return c;
    }
}

我从我的 aspx 页面调用方法如下:

$.ajax
(
    {
        type: "post",
        url: "NHibernateWebService.asmx/RetrieveEqualsClient",
        data: "{id: " + id + "}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        error: function (result) { alert("Failure: " + result.statusText); },
        success: function (result) { alert(result); }    
     }
 )

这给了我一个例外:NHibernate LazyInitializationException: failed to lazily initialize a collection, no session or session was closed, 这里 - //这是在客户端类中

public virtual IList<Project> projects 
{
    get { return c_projects ?? ( c_projects = new List<Project>()); }//Exception Occurs Here!
    set { c_projects = value; }
}

我针对此异常处理了各种问题,但无法使其发挥作用。

【问题讨论】:

    标签: c# jquery web-services nhibernate


    【解决方案1】:

    尝试为项目设置fetch="join"

    <bag name="projects" cascade ="all-delete-orphan" fetch="join"> 
      <key column="client_id" /> 
      <one-to-many class="Project" ></one-to-many> 
    </bag>
    

    【讨论】:

      【解决方案2】:

      您遇到的异常是由于您尝试在ISession 范围之外时加载项目集合。
      这可能会发生,因为在序列化时,序列化程序会读取该属性,并且它发生在您的 using 范围之外。
      最好的办法是use a dto instead of serializing your POCO

      所以你的代码看起来像:

      using (ISession session = m_SessionFactory.OpenSession())
          {
              ICriteria criteria = session.CreateCriteria(typeof(Client));
              criteria.Add(Expression.Eq(propertyName, propertyValue));
              var list = criteria.List<Client>();
              if (!list.Any())
                 return null;
              return new ClientDTO(list[0]);
          }
      

      此外,我不确定您通过从客户端获取属性名称进行的动态查询。
      您正在创建 UI 代码和数据库之间的强耦合(如果 Id 属性发生更改,这意味着您必须更改 jQuery 代码......)

      如果您的客户确实总是使用 Id 请求客户,那么Get&lt;Client&gt;(id) 方法会更合适且不易出错。

      【讨论】:

      • 我对设计网站和网络服务非常陌生,这是我第一次使用 NHibernate。我不知道 DTO 是什么或如何使用它。我确实使用了一种解决方法解决了这个问题——我调用了另一个名为EditClient() 的函数,它又调用了上面提到的RetrieveEqualsClient()。然后,我从检索到的客户端对象创建了一个 Json 字符串。我明白你的解释。但是为什么只有在项目集合的get方法中才会出现这种情况。为什么不使用 id、name 和 email 等简单属性?
      猜你喜欢
      • 2011-08-24
      • 1970-01-01
      • 2021-11-12
      • 2013-01-17
      • 1970-01-01
      • 1970-01-01
      • 2023-04-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多