2010-12-08 5 views
0

내 테이블 중 하나가 자체 매핑되는 상황이 있습니다. 한 행의 기본 키 (상위)는 다른 행 (하위)에 대한 외래 키로 사용될 수 있으며이 외래 키 열에는 상위가없는 행에 대해 null이 포함됩니다. 이런 식으로 뭔가 :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>();   
     } 
} 

매핑 : 내가 NULL을 포함하는 행을 검색에 문제가 (또는 말을하고 이제 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(); 
     } 
    } 

없습니다 자식)을 PatentLocationType 필드에 추가합니다. 나는이 repo.Get("ParentLocationType.LocationTypeId", null);처럼 null을 전달하려고하지만

답변

0

OK, 나는 그런 LocationType

0

object reference is not set to an instance error. 당신이 시도 가지고 일을 일부러하지만 던졌다 :

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

에 대한 쿼리 할 때 Expression.IsNull 대신 Expression.Eq의 사용을 해결 그래 내가 그랬어 그것도 작동하지 않았지만 나는 왜 그런지 알고 있다고 생각한다. "SELECT LocationId, Name, ParentLocationId FROM Settings_LocationType Where ParentLocationId = null"을 생성하는 SQL 쿼리를 살펴 본다. 나는 그것이 "= null"을 "null"로 대체하도록하는 방법이 있다면 해결할 수 있다고 생각합니다. 어떤 생각을 어떻게 할 것인가? – Waqas

+0

오케이, Expression.Eq 대신 Expression.IsNull을 사용하여 해결했습니다. – Waqas