サブロケーションのリストを持つオブジェクト「ロケーション」があります。私のオブジェクトとマッピングはこのようになっています
private int _id;
private string _name;
private IList<Location> _subLocations;
private IList<Stock> _stockList;
private Location _parent;
private bool _isActive;
private bool _recommend;
public virtual IList<Location> SubLocations
{
get
{
if (_subLocations == null)
{
_subLocations = new List<Location>();
}
return _subLocations;
}
set
{
_subLocations = value;
OnPropertyChanged("SubLocations");
}
}
// more properties ...
そして、私のマッピングは次のようになります。
public class LocationMap:ClassMap<Location>
{
public LocationMap()
{
Id(x => x.Id);
Map(x => x.Name);
Map(x => x.IsActive);
References(x => x.Parent);
HasMany(x => x.SubLocations).Where(x => x.IsActive == true);
HasMany(x => x.StockList).Where(x => x.IsActive == true);
Table("tbl_locations");
}
}
また、場所オブジェクトが IsActive = true であることも 100% 確信しています。
しかし、Location オブジェクトを取得すると、SubLocations リストは常に空になります。これがどのように動作するかを知っている人はいますか?または、そのような問題をデバッグするアイデアはありますか?
編集 私のデータベースは次のようになります。
Id Name IsActive Parent_id
1 Magazijn A 1 NULL
2 Magazijn B 1 NULL
3 Gang A 1 2
4 Rek B 1 3
クエリ これは、すべての親の場所を取得するためのクエリです
public IList<Location> GetAllParentLocations()
{
var result = NHibernateHelper.Session.CreateQuery("from Location l fetch all properties where l.Parent is null and l.IsActive = true").List<Location>();
return (List<Location>)result ?? new List<Location>();
}
生成された SQL
NHibernate: select location0_.Id as Id15_, location0_.Name as Name15_, location0_.IsActive as IsActive15_, location0_.Parent_id as Parent4_15_ from tbl_locations location0_ where (location0_.Parent_id is null) and location0_.IsActive=1
NHibernate: SELECT sublocatio0_.Location_id as Location5_1_, sublocatio0_.Id as Id1_, sublocatio0_.Id as Id15_0_, sublocatio0_.Name as Name15_0_, sublocatio0_.IsActive as IsActive15_0_, sublocatio0_.Parent_id as Parent4_15_0_ FROM tbl_locations sublocatio0_ WHERE (sublocatio0_.IsActive = 1) and sublocatio0_.Location_id=@p0;@p0 = 1 [Type: Int32 (0)]
NHibernate: SELECT sublocatio0_.Location_id as Location5_1_, sublocatio0_.Id as Id1_, sublocatio0_.Id as Id15_0_, sublocatio0_.Name as Name15_0_, sublocatio0_.IsActive as IsActive15_0_, sublocatio0_.Parent_id as Parent4_15_0_ FROM tbl_locations sublocatio0_ WHERE (sublocatio0_.IsActive = 1) and sublocatio0_.Location_id=@p0;@p0 = 42 [Type: Int32 (0)]
NHibernate: SELECT sublocatio0_.Location_id as Location5_1_, sublocatio0_.Id as Id1_, sublocatio0_.Id as Id15_0_, sublocatio0_.Name as Name15_0_, sublocatio0_.IsActive as IsActive15_0_, sublocatio0_.Parent_id as Parent4_15_0_ FROM tbl_locations sublocatio0_ WHERE (sublocatio0_.IsActive = 1) and sublocatio0_.Location_id=@p0;@p0 = 60 [Type: Int32 (0)]
クエリでは、「Location_id」列で where 句を実行します。ただし、Parent_id 列を使用する必要があります。この Location_id 列がどこから来ているのかわからない...