Persisting simple tree with (Fluent-)NHibernate leads to System.InvalidCastException
Posted
by
fudge
on Programmers
See other posts from Programmers
or by fudge
Published on 2010-12-29T19:48:24Z
Indexed on
2010/12/29
20:00 UTC
Read the original article
Hit count: 251
Hi there,
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
© Programmers or respective owner