在NHibernate中使用子类时出现“执行多条件时出错”异常

在NHibernate中使用子类时出现“执行多条件时出错”异常,nhibernate,subclass,nhibernate-inheritance,Nhibernate,Subclass,Nhibernate Inheritance,我试图使用NH将以下类映射到下表: public abstract class Person { public string SSN { get; set; } public string FirstName { get; set; } public string LastName { get; set; } } public class Customer : Person { public string ClubMemberId { get; set; }

我试图使用NH将以下类映射到下表:

public abstract class Person
{
    public string SSN { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
}

public class Customer : Person
{
    public string ClubMemberId { get; set; }
    //...
}

public class Employee : Person
{
    public string Department { get; set; }
    public int BankAccountNumber { get; set; }
    public int BankId { get; set; }
}

public class Cashier : Employee
{
    // no additional properties here (only methods)
}
表:

Persons:
  SSN (Primary Key)
  FirstName
  LastName
  PersonType  (discriminator: values can be Customer, Employee or Cashier)
  ClubMemberId

Employees:
  PersonSSN (Primary key and foriegn key to Persons.SSN
  Department
  BankAccountNumber
  BankId
以下是我尝试使用的hbm:

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
               namespace="BusinessLogic"
               assembly="BusinessLogic"
               default-access="property"
               default-lazy="false">

<class name="Person" table="Persons" discriminator-value="None">
  <id name="SSN" />
  <discriminator column="PersonType" />
  <property name="FirstName" />
  <property name="LastName" />

  <subclass name="Customer" discriminator-value="Customer">
    <property name="ClubMemberId" />
  </subclass>

  <subclass name="Employee" discriminator-value="Employee" >
    <join table="Employees">
      <key column="PersonSSN" />
      <property name="EmployeeId" />
      <property name="BankAccountNumber" />
      <property name="BankId" />
    </join>
  </subclass>

  <subclass name="Cashier" discriminator-value="Cashier" extends="Employee" />
    <!--<join table="Employees">
      <key column="PersonSSN" />
      <property name="CashierDummyProperty" />
    </join>
  </subclass>-->
</class>

</hibernate-mapping>
当我尝试使用以下代码加载所有员工记录“员工”和“出纳”时:

private ObservableCollection<T> LoadCollection<T>()
   where T : class
{
    var records = _session.QueryOver<T>().Future();
    return new ObservableCollection<T>(records);
}
其中T是Employee,我得到以下异常:

NHibernate.HibernateException: Error executing multi criteria : [SELECT this_.SSN as SSN1_0_, this_.FirstName as FirstName1_0_, this_.LastName as LastName1_0_, this_1_.EmployeeId as EmployeeId2_0_, this_1_.BankAccountNumber as BankAcco3_2_0_, this_1_.BankId as BankId2_0_ FROM Persons this_ inner join Employees this_1_ on this_.SSN=this_1_.PersonSSN WHERE this_.PersonType='Employee';
SELECT this_.SSN as SSN1_0_, this_.FirstName as FirstName1_0_, this_.LastName as LastName1_0_ FROM Persons this_ WHERE this_.PersonType='Cashier';
] ---> System.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index
at System.ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument argument, ExceptionResource resource)
at System.ThrowHelper.ThrowArgumentOutOfRangeException()
at System.Collections.Generic.List`1.get_Item(Int32 index)
at NHibernate.Impl.MultiCriteriaImpl.GetResultsFromDatabase(IList results) in d:\\CSharp\\NH\\nhibernate\\src\\NHibernate\\Impl\\MultiCriteriaImpl.cs:line 220
--- End of inner exception stack trace ---
at NHibernate.Impl.MultiCriteriaImpl.GetResultsFromDatabase(IList results) in d:\\CSharp\\NH\\nhibernate\\src\\NHibernate\\Impl\\MultiCriteriaImpl.cs:line 259
at NHibernate.Impl.MultiCriteriaImpl.DoList() in d:\\CSharp\\NH\\nhibernate\\src\\NHibernate\\Impl\\MultiCriteriaImpl.cs:line 171
at NHibernate.Impl.MultiCriteriaImpl.ListIgnoreQueryCache() in d:\\CSharp\\NH\\nhibernate\\src\\NHibernate\\Impl\\MultiCriteriaImpl.cs:line 143
at NHibernate.Impl.MultiCriteriaImpl.List() in d:\\CSharp\\NH\\nhibernate\\src\\NHibernate\\Impl\\MultiCriteriaImpl.cs:line 91
at NHibernate.Impl.FutureCriteriaBatch.GetResultsFrom(IMultiCriteria multiApproach) in d:\\CSharp\\NH\\nhibernate\\src\\NHibernate\\Impl\\FutureCriteriaBatch.cs:line 24
at NHibernate.Impl.FutureBatch`2.GetResults() in d:\\CSharp\\NH\\nhibernate\\src\\NHibernate\\Impl\\FutureBatch.cs:line 73
at NHibernate.Impl.FutureBatch`2.get_Results() in d:\\CSharp\\NH\\nhibernate\\src\\NHibernate\\Impl\\FutureBatch.cs:line 29
at NHibernate.Impl.FutureBatch`2.GetCurrentResult[TResult](Int32 currentIndex) in d:\\CSharp\\NH\\nhibernate\\src\\NHibernate\\Impl\\FutureBatch.cs:line 79
at NHibernate.Impl.FutureBatch`2.<>c__DisplayClass4`1.<GetEnumerator>b__3() in d:\\CSharp\\NH\\nhibernate\\src\\NHibernate\\Impl\\FutureBatch.cs:line 63
at NHibernate.Impl.DelayedEnumerator`1.<get_Enumerable>d__0.MoveNext() in d:\\CSharp\\NH\\nhibernate\\src\\NHibernate\\Impl\\DelayedEnumerator.cs:line 26
at System.Collections.ObjectModel.ObservableCollection`1.CopyFrom(IEnumerable`1 collection)
at System.Collections.ObjectModel.ObservableCollection`1..ctor(IEnumerable`1 collection)
at BusinessLogic.DataProvider.LoadCollection[T]() in C:\\Users\\Arnon\\Documents\\Visual Studio 2008\\Projects\\NHibernateDemo\\BusinessLogic\\DataProvider.cs:line 58
at BusinessLogic.DataProvider..ctor() in C:\\Users\\Arnon\\Documents\\Visual Studio 2008\\Projects\\NHibernateDemo\\BusinessLogic\\DataProvider.cs:line 28
我的其他发现:

如果我向出纳类添加一个新属性,NH将在Persons中查找相应字段,而不是在Employees中查找。我将其添加到我预期的Employees中

如果我在Cashier子类元素中显式地指定Employees表的相同联接以添加附加属性,则会得到上面提到的相同异常

我做错了什么


PS:IMHO,即使我在这里做错了什么,NH也应该在这里提供一个更具描述性的异常。

这是NHibernate MultiQueryImpl代码中的一个bug,请参阅。它无法成功处理多态查询