NHibernate 异常:无法初始化集合,列名无效。流畅的映射。也许是多对一的问题?

2024-03-11

我对通过 NHibernate 遇到的异常感到困惑和沮丧。对于这篇文章的篇幅,我深表歉意,但我已尝试提供适当的详细信息来充分解释该问题,以获得一些帮助!

事实如下:

  • 我有一个Person包含属性的类BillingManager,这也是一个Person类型。我将其映射为 FNH“参考”。
  • 我有一个ExpenseReport包含属性的类SubmittedBy,这是一个Person类型。我将其映射为 FNH“参考”。
  • 我有一个BillableTime包含属性的类Person,这是一个Person类型。我将其映射为 FNH“参考”。
  • Person包含一个集合 (IList)ExpenseReport类型(属性ExpenseReports)
  • Person包含一个集合 (IList)BilledTime类型(属性Time)

(请参阅帖子底部的类和映射。)

一切都很酷,直到我添加IList<BilledTime> Time收集到Person。现在,当我尝试访问时_person.Time,我得到一个例外:

代码:

// Get billable hours
if (_person.Time == null || 
    _person.Time.Count(x => x.Project.ProjectId == project.ProjectId) == 0)
{
    // No billable time for this project
    billableHours = Enumerable.Repeat(0F, 14).ToArray();
}

例外情况:

could not initialize a collection: 
[MyApp.Business.Person.Time#211d3567-6e20-4220-a15c-74f8784fe47a]
[SQL: SELECT 
time0_.BillingManager_id as BillingM8_1_, 
time0_.Id as Id1_, 
time0_.Id as Id1_0_, 
time0_.ReadOnly as ReadOnly1_0_, 
time0_.DailyHours as DailyHours1_0_, 
time0_.Week_id as Week4_1_0_, 
time0_.Person_id as Person5_1_0_, 
time0_.Project_id as Project6_1_0_, 
time0_.Invoice_id as Invoice7_1_0_ 
FROM [BillableTime] time0_ 
WHERE time0_.BillingManager_id=?]

确实如此BillingManager_id是无效的列名,它不存在于BillableTime桌子。但是,我不明白为什么 NHB 创建了这个 SQL...对我来说没有意义。在寻找解决方案时,我经常看到“无效的列名”异常,但似乎没有任何效果。更令人困惑的是:像BilledTime, the ExpenseReport类型还包含对Person它工作得很好。

我能够弄清楚的一件事是,如果我从 Person 映射中删除 BillingManager 引用(References(p => p.BillingManager)),异常消失并且一切似乎正常(相对于 BillableTime;它当然破坏了 BillingManager 的持久性)。现在看来存在一些“自我引用”问题,因为Person.BillingManager属性本身就是对 a 的引用Person.

知道这是怎么回事吗?我很茫然...

Thanks.

=== 类和映射 ===

public class Person
{
    public virtual string LastName { get; set; }
    public virtual string FirstName { get; set; }

    public virtual Person BillingManager { get; set; }

    public virtual IList<ExpenseReport> ExpenseReports { get; set; }
    public virtual IList<BillableTime> Time { get; set; }
}


public class PersonMapping : ClassMap<Person> 
{        
    public PersonMapping()
    {
        Id(p => p.UserId).GeneratedBy.Assigned();
        Map(p => p.LastName).Not.Nullable();
        Map(p => p.FirstName).Not.Nullable();

        References(p => p.BillingManager);

        HasMany(p => p.ExpenseReports).Cascade.AllDeleteOrphan();
        HasMany(p => p.Time).Cascade.AllDeleteOrphan(); 
    }
}

public class BillableTime
{
    public virtual int Id { get; private set; }
    public virtual Week Week { get; set; }
    public virtual Person Person { get; set; }
    public virtual Project Project { get; set; }
    public virtual float[] DailyHours { get; set; }
    public virtual Invoice Invoice { get; set; }
    public virtual bool ReadOnly { get; set; }
}       

public class BillableTimeMapping : ClassMap<BillableTime>
{
    public BillableTimeMapping()
    {
        Id(x => x.Id);
        References(x => x.Week);
        References(x => x.Person);
        References(x => x.Project);
        References(x => x.Invoice);
        Map(x => x.ReadOnly).Not.Nullable().Default("0");
        Map(x => x.DailyHours).Length(28);  
    }
}


public class ExpenseReport
{
    public virtual long Id { get; set; }        
    public virtual Person SubmittedBy { get; set; }             
}

以下行应该可以解决问题,但我不知道为什么会发生这种情况。如果我有空闲时间我会调查一下。

HasMany(p => p.Time).Cascade.AllDeleteOrphan().KeyColumn("Person_Id");
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

NHibernate 异常:无法初始化集合,列名无效。流畅的映射。也许是多对一的问题? 的相关文章

随机推荐