流畅的Nhibernate单向映射 - 会话重启后的ObjectNotFoundException

时间:2012-01-27 15:21:28

标签: c# nhibernate fluent-nhibernate

线有一个多边形,多边形有很多点。 无法在其他会话中加载数据(重新启动应用程序后) - Polygon.Points会抛出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);
    }

}

此外,我无法使用双向关系,因为我有另一个实体 - Word - 与Polygon有一对多的关系。

如何解决这个问题?

1 个答案:

答案 0 :(得分: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);
            }
        }