我正在尝试使用 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 是员工),我得到以下异常:
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
我的其他发现:
如果我向 Cashier 类添加一个新属性,NH 会在 Persons 中而不是在Employees 中查找相应的字段(我将它添加到了我期望的Employees 中)。
如果我在 Cashier 子类元素中明确指定Employees 表的相同连接(为了添加附加属性),我会得到上面提到的相同异常。
我在这里做错了什么?
PS:恕我直言,即使我在这里做错了,NH 也应该在这里提供一个更具描述性的例外。