3

次のクラス構造を検討してください。

public abstract class Animal
{
    public string SomeData { get; set; }
    public IList<AnimalProperty> AnimalProperties { get; set; }
    public IList<OtherAnimalProperty> OtherAnimalProperties { get; set; }
}

public class Dog: Animal
{
    public DogProperty DogProperties { get; set; }
}

Dog クラスの次のマッピングを使用

HasOne(x => x.DogProperties).ForeignKey("Id");

動物型のオブジェクトのリストを取得する次のクエリがあります。

list = session.QueryOver<Animal>()
       .WhereRestrictionOn(e => e.SomeData).IsIn(someList.ToArray())
       .Fetch(e => e.AnimalProperties).Default
       .Fetch(e => e.OtherAnimalProperties).Default
       .List();

リストに Dog 型のオブジェクトが含まれるようになりました (予想どおり) が、DogProperties プロパティはnull. つまり、マッピングで定義された 1 対 1 のクラスが初期化されていません。ただし、NHibernate は、データベースから DogProperty データも取得する優れた結合クエリを生成しました。このクエリを使用してクラスを作成しないだけです。

SELECT this_.Id as Id0_1_, this_.Name as Name0_1_, this_.DogName as DogName0_1_,
       this_.AnimalType as AnimalType0_1_, dogpropert2_.Id as Id1_0_, 
       dogpropert2_.MyProperty as MyProperty1_0_ 
FROM [Animal] this_ left outer join [DogProperties] dogpropert2_ 
       on this_.Id=dogpropert2_.Id

ここで私が見逃していることを誰か詳しく説明できますか? それとも、これは NHibernate QueryOver のバグ/デフォルトの動作ですか?

4

0 に答える 0