使用 (Fluent-)NHibernate 持久化简单树会导致 System.InvalidCastException
递归数据结构和 (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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
郑重声明:我通过用“ChildReferences”关系替换“Children”关系来回避这个问题,将实际的“Children”保留为瞬态(这些是通过“ChildReferences”间接构建的
......
)
: 映射:
...
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):
...
...
in the mapping:
...