NHibernate异常“对象引用未保存的瞬态实例 - 在刷新之前保存瞬态实例”使用HasMany保存对象时

时间:2012-01-26 01:32:43

标签: nhibernate nhibernate-mapping fluent-nhibernate-mapping

我正在尝试保存具有多个HasMany关系的对象,并且我得到异常:“对象引用未保存的瞬态实例 - 在刷新之前保存瞬态实例”。

下面是我的简化类,它们对应的映射和我的“应用程序”代码。

“应用程序代码”部分显示了我要执行的操作:将费用报表和工作时间添加到发票中,然后保存发票。

但是,GetTimeWorked()中会发生异常。如果我颠倒了订单(在费用报告之前添加了工作时间),则GetExpenseReports()中会出现错误。

如果我在添加费用报告后保存发票,然后在我添加工作时间后再次保存,它可以正常工作。但是,此保存需要是事务性的:费用报告和工作时间必须保存在一起。

我已经阅读了很多关于这个例外的内容,但我尝试的都没有。我读到的情况似乎与此略有不同。我猜这是一个映射问题,我尝试了一些替代映射(在HasMany方面,使用Cascade),但我不知所措。

知道这里发生了什么以及如何解决它?

谢谢!

// Classes
public class TimeWorked {
    public virtual long Id { get; private set; }
    public virtual float Hours { get; set; }
    public virtual Invoice Invoice { get; set; }
}

public class ExpenseReport {
    public virtual long Id { get; set; }
    public virtual IList<Expense> Expenses { get; set; }                
    public virtual Invoice Invoice { get; set; }
}

public class Invoice {
    public virtual long Id { get; set; }
    public virtual IList<ExpenseReport> ExpenseReports { get; set; }
    public virtual IList<TimeWorked> BilledTime { get; set; }

    public virtual void AddExpenseReport(List<ExpenseReport> expenseReports)
    {
        foreach (ExpenseReport er in expenseReports)
        {
            ExpenseReports.Add(er);
            er.Invoice = this;
        }
    }

    public virtual void AddTimeWorked(List<TimeWorked> timeWorked)
    {
        foreach (TimeWorked tw in timeWorked)
        {
            BilledTime.Add(tw);
            tw.Invoice = this;
         }
    }       
}

// Mapping
public class TimeWorkedMapping : ClassMap<TimeWorked>
{
    public TimeWorkedMapping()
    {
        Id(x => x.Id);
        References(x => x.Invoice);
    }
}

public class ExpenseReportMapping : ClassMap<ExpenseReport>
{
    public ExpenseReportMapping()
    {
        // Primary Key
        Id(x => x.Id);
        HasMany(x => x.Expenses).Cascade.AllDeleteOrphan();
            References(x => x.Invoice);
    }
}

public class InvoiceMapping : ClassMap<Invoice>
{
    public InvoiceMapping()
    {
        Id(x => x.Id);      
        HasMany(x => x.ExpenseReports).Inverse();
        HasMany(x => x.BilledTime).Inverse();
    }
}


// Application Code

public class MyPage
{
    // Do stuff...
    Invoice invoice = new Invoice();

    // Add the expense reports                       
    List<ExpenseReport> erList = GetExpenseReports();
    invoice.AddExpenseReport(erList);

    // Add billable time
    List<TimeWorked> twList = GetTimeWorked();     <<== Exception occurs in here
    invoice.AddTimeWorked(twList);

    // Save invoice
    Save(invoice);

} 

2 个答案:

答案 0 :(得分:1)

在创建新发票之前获取列表,默认发票ID可能存在问题。

List<TimeWorked> twList = GetTimeWorked();
List<ExpenseReport> erList = GetExpenseReports();
Invoice invoice = new Invoice();

// Add the expense reports                       
invoice.AddExpenseReport(erList);

// Add billable time  
invoice.AddTimeWorked(twList);

// Save invoice
Save(invoice);

答案 1 :(得分:0)

设置session.Flushmode == FlushMode.Commit会有所帮助。我猜GetExpenses()GetTimeWorked()引用发票会导致刷新以获得正确的数据