2011-10-14 72 views
0

我有一個問題,用NHibernate保存對象。下面的報表變量沒有變化,但NHibernate的拋出下面的例外:NHibernate會話,saveorupdate,保存

具有相同標識符的值不同的物體已經與所述會話相關聯:262的實體,:xxx.Report

pulic void SaveReport(Report report) 
    { 
     using (ISession session = NH.OpenSession()) 
     using (ITransaction transaction = session.BeginTransaction()) 
     { 

      var childReport = session.QueryOver<Report>() 
      .Where(x => x.ReportParent.Id == report.Id) 
      .Fetch(x => x.Children).Eager 
    .List().Distinct().ToList(); -- [1] refers to the line starting with .List() 

      --[2] report.Children = report.Children; 

       session.SaveOrUpdate(report); 
       transaction.Commit(); 

        } 
     } 


      class Report 
      { 
      public virtual int Id { get; set; } 
      public virtual IList<Report> Children { get; set; } 
      public virtual Report ReportParent { get; set; } 
      } 

如果行[1]被註釋掉了,沒有例外,但我需要在保存它之前對報表變量進行更改。例如,使用該線下方上線[2]:

  report.Children = report.Children; 

UPDATE:報告是使用其他會話SaveReport方法外部加載,和session.dispose()被調用。

誰能告訴我

1)爲什麼NHibernate的拋出例外,甚至報告變量沒有變化,

2)爲什麼當行[1]被註釋掉也不例外。

預先感謝您!

+1

一個顯而易見的問題是:「report」是從檢索它的會話中分離出來的嗎? –

+1

報告在使用其他會話的SaveReport方法之外加載。 – Pingpong

回答

1

在您的評論之後:您輸入一個Report對象,該對象可能仍然附加到檢索的會話。您必須首先將其從該會話中分離出來(Session.Evict)。

+0

但session.dispose()已在檢索報表對象的會話上調用,這意味着應該處理該會話。 – Pingpong