其他分享
首页 > 其他分享> > 由于抽象方法,NHibernate创建代理实例失败

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

作者:互联网

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

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

SetRequired是一种这样的方法,在我的Question类中它是一个空值,并且在所有地方都覆盖了它,但是当我尝试测试映射时,出现此错误:

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>

解决方法:

我相信关键是要声明这样的方法:受保护的内部抽象,因为这将允许代理从它们继承.您阅读了有关此here的更多信息.

标签:hibernate,nhibernate,nhibernate-mapping,c
来源: https://codeday.me/bug/20191209/2096631.html