nHibernateでTableper具体的な方法を使用して次のクラス階層のxmlマッピングファイルを作成する際に問題が発生しました。「間違ったクラス例外」エラーが発生し続けます。誰かが私を正しい方向に向けるのを手伝ってもらえますか?
これは私の抽象/親クラスの定義です:
public abstract partial class AlertSpecification
{
private long _alertSpecificationId;
private string _specificationName;
private bool _active;
private int _createdBy;
private DateTime _createdOn;
public virtual long AlertSpecificationId
{
get { return _alertSpecificationId; }
set { _alertSpecificationId = value; }
}
public virtual string SpecificationName
{
get { return _specificationName; }
set { _specificationName = value; }
}
public virtual bool Active
{
get { return _active; }
set { _active = value; }
}
public virtual int CreatedBy
{
get { return _createdBy; }
set { _createdBy = value; }
}
public virtual DateTime CreatedOn
{
get { return _createdOn; }
set { _createdOn = value; }
}
}
これは私の最初の子クラスの定義です:
public partial class ComponentSpecification : AlertSpecification
{
private string _vehicleType;
public virtual string VehicleType
{
get { return _vehicleType; }
set { _vehicleType = value; }
}
}
これは私の2番目の子クラスの定義です:
public partial class ColdVehicleSpecification : AlertSpecification
{
private double _sigmaThreshold;
public virtual double SigmaThreshold
{
get { return _sigmaThreshold; }
set { _sigmaThreshold = value; }
}
}
これは私のマッピングファイルの定義です:
<?xml version="1.0" encoding="utf-8"?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
namespace="IS.QueryPerformanceTest.Model" assembly="IS.QueryPerformanceTest.Model" >
<class name="AlertSpecification" abstract="true">
<cache usage="read-write"/>
<id name="AlertSpecificationId" type="Int64">
<generator class="hilo"/>
</id>
<property name="SpecificationName" column="Name" />
<property name="Active" />
<property name="CreatedBy" />
<property name="CreatedOn" />
<union-subclass name="ColdVehicleSpecification" table="AlertSpecificationColdVehicle">
<property name="SigmaThreshold" column="CVSigmaThreshold" />
</union-subclass>
<union-subclass name="ComponentSpecification" table="AlertSpecificationComponent">
<property name="VehicleType" column="VehicleType" />
</union-subclass>
</class>
</hibernate-mapping>
私のコントローラーでは、次のコードを使用してデータを取得します。
var repo = new NHComponentSpecificationRepository(ObjectFactory.GetInstance<ISession>
());
var cvRepo = new
NHColdVehicleSpecificationRepository(ObjectFactory.GetInstance<ISession>());
var allComponentSpecs = repo.FindAll();
var allColdVehicleSpecs = cvRepo.FindAll();
これは私が得ているエラーです:ID:1のオブジェクトは指定されたサブクラスではありませんでした:IS.QueryPerformanceTest.Model.ColdVehicleSpecification(ロード中のオブジェクトは間違ったクラス[IS.QueryPerformanceTest.Model.ComponentSpecification]でした)
repo.FindAll()を最初に呼び出すと、正しいタイプで正常に返されることに注意してください。ただし、2番目の呼び出しは失敗していました。いくつかの理由で、ColdVehicleSpecificationをロードするようにコードで指定した場合でも、常にComponentSpecificationをロードしようとします。
これは、NHColdVehicleSpecificationRepositoryでのFindAllの実装です。
public IList<ColdVehicleSpecification> FindAll()
{
var result = _session.Query<ColdVehicleSpecification>().ToList();
return result;
}
誰かがここで何が悪いのか手がかりを持っていますか?
さらに詳しい情報が必要な場合はお知らせください。
前もって感謝します。