2012-01-27 62 views
0

行有一個多邊形,多邊形有很多Point。 無法在其他會話中加載數據(在重新啓動應用程序之後) - Polygon.Points拋出ObjectNotFoundException。 當我在一個會話中更新並重新加載父實體時,所有工作都正常。Fluent Nhibernate單向映射 - 會話重啓後的ObjectNotFoundException

映射:

public class LineMap : ClassMap<Line> 
{ 
    public LineMap() 
    { 
     Id(x => x.UID).GeneratedBy.Guid(); 
     Map(x => x.Number); 
     HasMany(x => x.Words) 
      .Cascade.All() 
      .Inverse(); 
     References(x => x.Page); 
     HasOne(x => x.Polygon) 
      .Constrained() 
      .ForeignKey() 
      .Cascade.All(); 
    } 
} 

public class WordMap : ClassMap<Word> 
{ 
    public WordMap() 
    { 
     Id(x => x.UID).GeneratedBy.Guid(); 
     Map(x => x.Name); 
     Map(x => x.Number); 
     HasMany(x => x.Polygons) 
      .Not.Inverse()  
      .Not.KeyNullable() 
      .Cascade.All(); 
     References(x => x.Line); 
    } 
} 

public class PolygonMap : ClassMap<Polygon> 
{ 
    public PolygonMap() 
    { 
     Id(x => x.UID).GeneratedBy.Guid(); 
     HasMany(x => x.Points) 
      .Cascade.All() 
      .Inverse(); 
    } 
} 

public class PointMap : ClassMap<Point> 
{ 
    public PointMap() 
    { 
     Id(x => x.UID).GeneratedBy.Guid(); 
     Map(x => x.X); 
     Map(x => x.Y); 
     References(x => x.Polygon); 
    } 

} 

此外,我不能使用雙向關係,因爲我還有一個實體 - 字 - 有一個一對多的關係多邊形。

如何解決這個問題?

回答

0

更改映射到

public class LineMap : ClassMap<Line> 
{ 
    public LineMap() 
    { 
     Id(x => x.UID).GeneratedBy.Guid(); 
     Map(x => x.Number); 
     HasMany(x => x.Words) 
      .Cascade.All() 
      .Inverse(); 
     References(x => x.Page); 
     HasMany(x => x.Polygon) 
      .Cascade.All(); 
    } 
} 

public class WordMap : ClassMap<Word> 
{ 
    public WordMap() 
    { 
     Id(x => x.UID).GeneratedBy.Guid(); 
     Map(x => x.Name); 
     Map(x => x.Number); 
     HasMany(x => x.Polygons) 
      .Cascade.All(); 
     References(x => x.Line); 
    } 
} 

public class PolygonMap : ClassMap<Polygon> 
{ 
    public PolygonMap() 
    { 
     Id(x => x.UID).GeneratedBy.Guid(); 
     HasMany(x => x.Points) 
      .Cascade.All() 
      .Inverse(); 
    } 
} 

public class PointMap : ClassMap<Point> 
{ 
    public PointMap() 
    { 
     Id(x => x.UID).GeneratedBy.Guid(); 
     Map(x => x.X); 
     Map(x => x.Y); 
     References(x => x.Polygon); 
    } 

} 

,並添加到域代碼:

public virtual Polygon Polygon 
     { 
      get 
      { 
       if (Polygons.Count == 0) return null; 
       return Polygons[0]; 
      } 
      set 
      { 
       if(Polygons.Count == 0) 
       { 
        Polygons.Add(value); 
        return; 
       } 
       Polygons[0]=(value); 
      } 
     } 
相關問題