2010-12-08 26 views
0

我有一种情况,我的表中的一个自映射到自身。一行(Parent)的主键可以用作其他行(Child)的外键,并且对于没有父行的这样的行,该外键列包含null。事情是这样的:检索具有空值的记录 - 流畅的nhibernate

table: Settings_LocationType 

++++++++++++++++++++++++++++++++++++++++++++++++ 
LocationID | Name  | ParentLocationId 
++++++++++++++++++++++++++++++++++++++++++++++++ 
1   Parent 1 null 
2   Child 1 1 
3   Child 2 1 
4   Parent 2 null 

型号:的locationType

public class LocationType 
{ 
     public virtual long LocationTypeId { get; set; } 
     public virtual string Name { get; set; } 
     public virtual LocationType ParentLocationType { get; set; } 
     public virtual IList<LocationType> LocationTypes { get; set; } 

     public LocationType() 
     { 
      LocationTypes = new List<LocationType>();   
     } 
} 

映射:LocationTypeMap

public class LocationTypeMap : ClassMap<LocationType> 
    { 
     public LocationTypeMap() 
     { 
      Table("Setting_LocationType"); 
      Id(x => x.LocationTypeId).Column("LocationId").GeneratedBy.Sequence("location_type_seq"); 
      Map(x => x.ShortName, "Name").Length(15).Not.Nullable(); 
      References<LocationType>(x => x.ParentLocationType).LazyLoad().Nullable();    
      HasMany<LocationType>(x => x.LocationTypes).AsBag().KeyColumn("ParentLocationId").KeyNullable().LazyLoad().Inverse().Cascade.SaveUpdate(); 
     } 
    } 

现在我在检索包含NULL的那些行有一个问题(或者说是不子)在PatentLocationType字段中。我尝试空传递这样repo.Get("ParentLocationType.LocationTypeId", null);但它没有工作,但扔object reference is not set to an instance error.

回答

0

OK,我解决了这个问题查询等LocationType

0

你试过:

repo.Get ("ParentLocationType", null) 
+0

时使用的Expression.IsNull代替Expression.Eq是我没有,它也没有工作,但我想我知道为什么:看看它生成的SQL查询“SELECT LocationId,Name,ParentLocationId FROM Settings_LocationType WHERE ParentLocationId = null”。我认为这可以解决,如果我如何使用“为null”来替换“= null”。任何想法如何做到这一点? – Waqas 2010-12-08 11:07:39