使用 (Fluent-)NHibernate 持久化简单树会导致 System.InvalidCastException

发布于 2024-10-09 11:57:22 字数 1412 浏览 3 评论 0原文

递归数据结构和 (Fluent-)NHibernate 似乎存在问题,或者只是我,是一个完全的白痴......

这是树:

public class SimpleNode {

public SimpleNode ()
{
    this.Children = new List<SimpleNode> ();
}

public virtual SimpleNode Parent { get; private set; }
public virtual List<SimpleNode> Children { get; private set; }

public virtual void setParent (SimpleNode parent)
{
    parent.AddChild (this);
    Parent = parent;
}

public virtual void AddChild (SimpleNode child)
{
    this.Children.Add (child);
}

public virtual void AddChildren (IEnumerable<SimpleNode> children)
{
    foreach (var child in children) {
        AddChild (child);
    }
}

}

映射:

public class SimpleNodeEntity : ClassMap;

{

public SimpleNodeEntity ()
{
    Id (x => x.Id);

    References (x => x.Parent).Nullable ();

    HasMany (x => x.Children).Not.LazyLoad ().Inverse ().Cascade.All ().KeyNullable ();
}

}

现在,每当我尝试保存节点时,我都会得到:

System.InvalidCastException:无法从源类型转换为目标类型。在(包装动态方法)SimpleNode。 (对象,对象[],NHibernate.Bytecode.Lightweight.SetterCallback)在NHibernate.Bytecode.Lightweight.AccessOptimizer.SetPropertyValues(对象,对象[])在NHibernate.Tuple.Entity.PocoEntityTuplizer.SetPropertyValuesWithOptimizer(对象,对象[])

我设置:

Mono 2.8.1(在 OSX 上)、NHibernate 2.1.2、FluentNHibernate 1.1.0

there seems to be a problem with recursive data structures and (Fluent-)NHibernate or its just me, being a complete moron...

here's the tree:

public class SimpleNode {

public SimpleNode ()
{
    this.Children = new List<SimpleNode> ();
}

public virtual SimpleNode Parent { get; private set; }
public virtual List<SimpleNode> Children { get; private set; }

public virtual void setParent (SimpleNode parent)
{
    parent.AddChild (this);
    Parent = parent;
}

public virtual void AddChild (SimpleNode child)
{
    this.Children.Add (child);
}

public virtual void AddChildren (IEnumerable<SimpleNode> children)
{
    foreach (var child in children) {
        AddChild (child);
    }
}

}

the mapping:

public class SimpleNodeEntity : ClassMap<SimpleNode>

{

public SimpleNodeEntity ()
{
    Id (x => x.Id);

    References (x => x.Parent).Nullable ();

    HasMany (x => x.Children).Not.LazyLoad ().Inverse ().Cascade.All ().KeyNullable ();
}

}

now, whenever I try to save a node, I get this:

System.InvalidCastException: Cannot cast from source type to destination type. at (wrapper dynamic-method) SimpleNode. (object,object[],NHibernate.Bytecode.Lightweight.SetterCallback) at NHibernate.Bytecode.Lightweight.AccessOptimizer.SetPropertyValues (object,object[]) at NHibernate.Tuple.Entity.PocoEntityTuplizer.SetPropertyValuesWithOptimizer (object,object[])

My setup:

Mono 2.8.1 (on OSX), NHibernate 2.1.2, FluentNHibernate 1.1.0

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

时间你老了 2024-10-16 11:57:22

郑重声明:我通过用“ChildReferences”关系替换“Children”关系来回避这个问题,将实际的“Children”保留为瞬态(这些是通过“ChildReferences”间接构建的

......

public virtual IList<ChildReference> ChildReferences { get; private set; }

: 映射:

HasMany (x => x.ChildReferences).Cascade.All ();

public class ChildReference 
    {
        public virtual int ChildId { get; set; }
    }  

...

For the record: I kinda fudged the problem by replacing the 'Children' relation by a 'ChildReferences' relation, leaving the actual 'Children' as transient (these get built via the 'ChildReferences' indirection):

...

public virtual IList<ChildReference> ChildReferences { get; private set; }

...

in the mapping:

HasMany (x => x.ChildReferences).Cascade.All ();

public class ChildReference 
    {
        public virtual int ChildId { get; set; }
    }  

...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文