0

我有一个大问题!在 nhibernate 中映射具有多个联合子类的抽象类时,我不断收到异常。当我只使用一个联合子类时,我没有收到任何错误并且一切正常。当我使用更多子类时,会发生此错误:

[TargetException:对象与目标类型不匹配。] System.Reflection.RuntimeMethodInfo.CheckConsistency(Object target) +10909543 System.Reflection.RuntimeMethodInfo.InvokeArgumentsCheck(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfoculture) + 115 System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfoculture) +54 System.Reflection.RuntimePropertyInfo.GetValue(Object obj, BindingFlags invokeAttr, Binder binder, Object[] index, CultureInfo 文化) +61 System.Reflection.RuntimePropertyInfo.GetValue(Object obj, Object[] index) +19 NHibernate.Properties.BasicGetter.Get(Object target) +99

[PropertyAccessException: MedControlWeb.Models.Logs.Log.Code 发生异常 getter] NHibernate.Properties.BasicGetter.Get(Object target) +183 NHibernate.Engine.UnsavedValueFactory.GetUnsavedIdentifierValue(String unsavedValue, IGetter identifierGetter, IType identifierType, ConstructorInfo 构造函数) +160 NHibernate.Tuple.PropertyFactory.BuildIdentifierProperty(PersistentClass mappedEntity, IIdentifierGenerator generator) +200 NHibernate.Tuple.Entity.EntityMetamodel..ctor(PersistentClass persistentClass, ISessionFactoryImplementor sessionFactory) +775 NHibernate.Persister.Entity.AbstractEntityPersister..ctor(PersistentClass persistentClass , ICacheConcurrencyStrategy 缓存, ISessionFactoryImplementor factory) +835 NHibernate.Persister.Entity.UnionSubclassEntityPersister..ctor(PersistentClass persistentClass, ICacheConcurrencyStrategy cache, ISessionFactoryImplementor factory, IMapping mapping) +220 NHibernate.Persister.PersisterFactory.CreateClassPersister(PersistentClass model, ICacheConcurrencyStrategy cache, ISessionFactoryImplementor factory, IMapping cfg) +369 NHibernate.Impl.SessionFactoryImpl..ctor(Configuration cfg, IMapping 映射、设置设置、EventListeners 监听器)+2199 NHibernate.Cfg.Configuration.BuildSessionFactory() +181ctor(Configuration cfg, IMapping mapping, Settings settings, EventListeners listeners) +2199 NHibernate.Cfg.Configuration.BuildSessionFactory() +181ctor(Configuration cfg, IMapping mapping, Settings settings, EventListeners listeners) +2199 NHibernate.Cfg.Configuration.BuildSessionFactory() +181

我的 hbm 文件:

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
                   assembly="MedControlWeb"
                   namespace="MedControlWeb.Models.Logs">

  <class name="Log" abstract="true" lazy="false">
    <id name="Code" column="code">
      <generator class="hilo"/>
    </id>

    <property name="Description" column="description"/>
    <property name="User" column="user"/>
    <property name="Timestamp" column="timestamp"/>
    <property name="Action" column="action"/>

    <union-subclass name="SettingsLog" table="settings_log" lazy="false">
    </union-subclass>

    <union-subclass name="JobLog" table="job_log" lazy="false">
    </union-subclass>

  </class>
</hibernate-mapping>

我的抽象超类:

public class Log
{
    public int Code { get; set; }
    public DateTime Timestamp { get; set; }
    public MedControlWeb.Enums.Action Action { get; set; }
    public string Description { get; set; }
    public int User { get; set; }
}

子类之一:

public class SettingsLog : Log
{        
}

我哪里错了?

编辑:我能做些什么来解决这个错误?由于我有正确的 get 方法,我不知道如何修复它?

4

1 回答 1

0

信不信由你,这里的解决方案非常简单。这是映射:

<class name="Log" ...
    ...
    <union-subclass name="SettingsLog" table="settings_log" lazy="false">
    </union-subclass>

    <union-subclass name="JobLog" table="job_log" lazy="false">
    </union-subclass>

这是 SettingsLog的片段上方:

public class SettingsLog : Log // this is it, SettingsLog is also a Log
{...}

这是有效的,当然,SettingsLog- 也是Log. 报告的问题并非来自以下事实:

...映射具有多个联合子类的抽象类时出现异常...

但事实上,另一个映射union-subclass的不是Log

public class JobLog // this is NOT a Log..
{...}

这就是为什么我们可以看到:

例外:对象与目标类型不匹配
(嗯 - 是的,因为JobLog不是Log

所以,只要确保它JobLogLog... 的子类,一切都会开始工作

public class JobLog : Log

于 2014-11-06T07:02:46.560 回答