NHibernate:具有id的对象不是指定的子类:(加载对象的类错误)

时间:2010-08-10 18:22:59

标签: nhibernate nhibernate-mapping linq-to-nhibernate

我收到以下错误:

ID为34dd93d3-374e-df11-9667-001aa03fa2c4的对象不属于指定的子类:MyNamespace.ClassA(加载对象的类错误[MyNamespace.ClassB1])

调用堆栈是:

at NHibernate.Loader.Loader.InstanceAlreadyLoaded(IDataReader rs,Int32 i,IEntityPersister persister,EntityKey key,Object obj,LockMode lockMode,ISessionImplementor session)
   在NHibernate.Loader.Loader.GetRow(IDataReader rs,ILoadable [] persisters,EntityKey [] keys,Object optionalObject,EntityKey optionalObjectKey,LockMode [] lockModes,IList hydratedObjects,ISessionImplementor session)
   在NHibernate.Loader.Loader.GetRowFromResultSet(IDataReader resultSet,ISessionImplementor session,QueryParameters queryParameters,LockMode [] lockModeArray,EntityKey optionalObjectKey,IList hydratedObjects,EntityKey [] keys,Boolean returnProxies)
   在NHibernate.Loader.Loader.DoQuery(ISessionImplementor session,QueryParameters queryParameters,Boolean returnProxies)
   在NHibernate.Loader.Loader.DoQueryAndInitializeNonLazyCollections(ISessionImplementor session,QueryParameters queryParameters,Boolean returnProxies)
   ...
   在NHibernate.Linq.CriteriaResultReader 1.<GetEnumerator>d__0.MoveNext()
at System.Collections.Generic.List
1..ctor(IEnumerable 1 collection)
at System.Linq.Enumerable.ToList[TSource](IEnumerable
1来源)
   ...

我已经重命名了我的类并取出了不相关的属性等,为了简化,但继承的级别与我的实际情况完全相同。

课程:

public abstract class BaseClass : 
{
    public Guid ID { get; set; }
    ...

}
public abstract class ClassA : BaseClass
{
    public ClassB RelatedObject { get; set; }
}
public abstract class ClassB : BaseClass
{
    public ClassA RelatedObject { get; set; }
}
public abstract class ClassC : BaseClass
{
    ...
}

public class ClassA1 : ClassA
{
    ...
}
public class ClassA2 : ClassA
{
    ...
}

public class ClassB1 : ClassB
{
    ...
}
public class ClassB2 : ClassB
{
    ...
}

public class ClassC1 : ClassC
{
    ...
}
public class ClassC2 : ClassC
{
    ...
}

有一个基类表,然后是一些具体类的单独表,而不是所有类。相关对象的相应列位于主表中。

BaseClassTable:
-------------------------------
| ID | Kind | RelatedObjectID |
-------------------------------

Individual tables:
---------------------------------
| ID | some specific properties |
---------------------------------

映射是:

<class name="MyNamespace.BaseClass, MyNamespace" table="BaseClassTable" discriminator-value="BC" abstract="true">
    <id name="ID" type="System.Guid" column="ID" unsaved-value="00000000-0000-0000-0000-000000000000">
      <generator class="guid" />
    </id>
    <discriminator column="Kind" type="System.String"/>
    ...
    <subclass name="MyNamespace.ClassA, MyNamespace" discriminator-value="A" abstract="true">
        <many-to-one name="RelatedObject" class="MyNamespace.ClassB, MyNamespace" column="RelatedObjectID"/>
    </subclass>
    <subclass name="MyNamespace.ClassB, MyNamespace" discriminator-value="B" abstract="true">
        <many-to-one name="RelatedObject" class="MyNamespace.ClassA, MyNamespace" column="RelatedObjectID"/>
    </subclass>
    <subclass name="MyNamespace.ClassC, MyNamespace" discriminator-value="C" abstract="true">
    ...
    </subclass>
  </class>
  <subclass name="MyNamespace.ClassA1, MyNamespace" extends="MyNamespace.ClassA, MyNamespace" discriminator-value="A1">
    <join table="ClassA1Table">
        <key column="ID"/>
        ...
    </join> 
    ...
  </subclass>
  <subclass name="MyNamespace.ClassA2, MyNamespace" extends="MyNamespace.ClassA, MyNamespace" discriminator-value="A2">
    ...
  </subclass>
  <subclass name="MyNamespace.ClassB1, MyNamespace" extends="MyNamespace.ClassB, MyNamespace" discriminator-value="B1">
    ...
  </subclass>
  <subclass name="MyNamespace.ClassB2, MyNamespace" extends="MyNamespace.ClassB, MyNamespace" discriminator-value="B2">
    ...
  </subclass>
  <subclass name="MyNamespace.ClassC1, MyNamespace" extends="MyNamespace.ClassC, MyNamespace" discriminator-value="C1">
    ...
      </subclass>
  <subclass name="MyNamespace.ClassC2, MyNamespace" extends="MyNamespace.ClassC, MyNamespace" discriminator-value="C2">
    ...
  </subclass>

当然,在DB中我只会为鉴别器提供A1,A2,B1,B2,C1和C2值。

之前有没有人碰到这样的事情?可以将属性映射为基类,然后为相同的ID加载具体类的实例吗?有没有更好的方法来实现这一目标? 谢谢!

这是抛出异常的调用:

尝试使用我的数据上下文中的IQueryable检索对象时抛出错误:

List<MyNamespace.BaseClass> items = context.MyObjects.ToList();

在我的数据上下文中,IQueryable属性如下所示:

IQueryable<MyNamespace.BaseClass> IMyDataContext.MyObjects
{
   get
   {
      return this.Session.Linq<MyNamespace.BaseClass>();
   }
}

0 个答案:

没有答案