2011-01-06 14 views
0

我試圖用NH映射以下類下面的表格使用子類時:「錯誤執行多標準」異常NHibernate的

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> 

當我嘗試使用fol加載所有員工記錄(員工和收銀員)降脂代碼:

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 

其他發現我有:

  1. 如果我添加一個新的屬性設置收銀員類,NH在人員中查找相應的字段,而不是在員工中(我將其添加到我期望的員工處)。

  2. 如果我明確指定收銀員子類元素內的Employees表的相同連接(爲了添加附加屬性),我會得到上述相同的異常。

我在做什麼錯在這裏? PS:恕我直言,即使我在這裏做錯了事,NH應該在這裏提供一個更具描述性的例外。

回答