由于抽象方法,NHibernate创建代理实例失败

时间:2010-09-16 03:37:00

标签: c# nhibernate hibernate nhibernate-mapping

我是NHibernate的新手,我正在尝试映射具有一些继承性的域模型等(有关我的模型的完整详细信息,请参阅this question,开始一个新问题,因为这是一个不同的错误)

我的基类有一些抽象方法,下面的每个类都必须实现。这似乎导致了NHibernate的问题,即使我在我的域模型中实现了这个类,NHibernates代理类似乎也遇到了麻烦。

一个这样的方法是SetRequired,这是我的Question类中的一个void,我在任何地方都覆盖了它,但是当我试图测试我的映射时,我收到了这个错误:

PmqccFormIntegrationTests.TestJobVelocityQuestionPersists : FailedNHibernate.HibernateException : Creating a proxy instance failed
  ----> System.TypeLoadException : Method 'SetRequired' in type 'QuestionProxy' from assembly 'QuestionProxyAssembly, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null' does not have an implementation.
at NHibernate.ByteCode.LinFu.ProxyFactory.GetProxy(Object id, ISessionImplementor session)
at NHibernate.Tuple.Entity.AbstractEntityTuplizer.CreateProxy(Object id, ISessionImplementor session)
at NHibernate.Persister.Entity.AbstractEntityPersister.CreateProxy(Object id, ISessionImplementor session)
at NHibernate.Event.Default.DefaultLoadEventListener.CreateProxyIfNecessary(LoadEvent event, IEntityPersister persister, EntityKey keyToLoad, LoadType options, IPersistenceContext persistenceContext)
at NHibernate.Event.Default.DefaultLoadEventListener.ProxyOrLoad(LoadEvent event, IEntityPersister persister, EntityKey keyToLoad, LoadType options)
at NHibernate.Event.Default.DefaultLoadEventListener.OnLoad(LoadEvent event, LoadType loadType)
at NHibernate.Impl.SessionImpl.FireLoad(LoadEvent event, LoadType loadType)
at NHibernate.Impl.SessionImpl.Load(String entityName, Object id)
at NHibernate.Impl.SessionImpl.Load(Type entityClass, Object id)
at PmqccDomain.DataAccess.NHibernate.AbstractNHibernateDao`2.GetById(IdT id, Boolean shouldLock) in AbstractNHibernateDao.cs: line 31
at PMQCC_IntegrationTests.PmqccFormIntegrationTests.TestJobVelocityQuestionPersists() in PmqccFormIntegrationTests.cs: line 92
--TypeLoadException
at System.Reflection.Emit.TypeBuilder.TermCreateClass(RuntimeModule module, Int32 tk, ObjectHandleOnStack type)
at System.Reflection.Emit.TypeBuilder.CreateTypeNoLock()
at System.Reflection.Emit.TypeBuilder.CreateType()
at LinFu.DynamicProxy.ProxyFactory.CreateUncachedProxyType(Type[] baseInterfaces, Type baseType)
at LinFu.DynamicProxy.ProxyFactory.CreateProxyType(Type baseType, Type[] baseInterfaces)
at LinFu.DynamicProxy.ProxyFactory.CreateProxy(Type instanceType, IInterceptor interceptor, Type[] baseInterfaces)
at NHibernate.ByteCode.LinFu.ProxyFactory.GetProxy(Object id, ISessionImplementor session) 

我已尝试在我的映射文件中设置'abstract =“true”',并且lazy =“false”但我仍然收到错误。思考?

编辑:这是我当前的映射

 <class name ="PmqccDomain.DomainObjects.Question,PmqccDomain" abstract="true" table="Questions">
    <id column="QuestionId" name="Id" type="Int32" unsaved-value="-1">
      <generator class="native"></generator>
    </id>
    <discriminator column="QuestionType" type="String" />
    <property name="Explanation"/>
    <many-to-one name="PmqccForm" class="PmqccDomain.DomainObjects.PmqccForm" column="PmqccFormId" />


    <!-- Subclass for Normal Question-->
    <subclass name="PmqccDomain.DomainObjects.NormalQuestion,PmqccDomain" abstract="true"  lazy="false">
      <property name="InputtedAnswer"/>

      <!-- Each Question Subclassed-->
      <subclass name="PmqccDomain.DomainObjects.AsConsQuestion,PmqccDomain" lazy="false" />
      <subclass name="PmqccDomain.DomainObjects.PiAlertQuestion,PmqccDomain" lazy="false">

        <many-to-one name="ResponsibleStaffMember" class="PmqccDomain.DomainObjects.StaffMember" column="ResponsibleStaffId" />

      </subclass>

    </subclass>

    <!-- Subclass for Job Velocity Question-->
    <subclass name="PmqccDomain.DomainObjects.JobVelocityQuestion,PmqccDomain" lazy="false">
      <property name="InputtedAnswer"/>

    </subclass>

  </class>

1 个答案:

答案 0 :(得分:3)

我认为关键是要声明这样的方法:protected internal abstract因为这将允许代理继承它们。您阅读了有关此here的更多信息。