如何在 NHibernate 中映射聚合关系的多个根类型?
假设我有一个 Child 类,它包含在不同的其他类中(本例中为 ParentA 和 ParentB)。以下简单案例演示了一个简单模型,其中 ParentA 和 ParentB 都包含一组 Child 对象。
public class ParentA
{
public virtual ISet<Child> Children { get; set; }
}
public class ParentB
{
public virtual ISet<Child> Children { get; set; }
}
public class Child
{
}
在这个简单的情况下,从父级到子级的导航没有问题。但现在我希望能够导航到父级。通常我会引入一个接口 IParent)
public class ParentA: IParent
{
public virtual ISet<Child> Children { get; set; }
}
public class ParentB
{
public virtual ISet<Child> Children { get; set; }
}
public class Child
{
public virtual IParent Parent { get; set; }
}
现在的问题是如何在 NHibernate 中映射这样的关系?
Let's say I have a class Child which is contained in different other classes (ParentA and ParentB in this example). The following simple case demonstrates a simple model where ParentA and ParentB both contain a set of Child objects.
public class ParentA
{
public virtual ISet<Child> Children { get; set; }
}
public class ParentB
{
public virtual ISet<Child> Children { get; set; }
}
public class Child
{
}
Navigation from parent to child is no problem in this simple case. But now I want to be able to navigate to the parent. Typically I would introduce an interface IParent)
public class ParentA: IParent
{
public virtual ISet<Child> Children { get; set; }
}
public class ParentB
{
public virtual ISet<Child> Children { get; set; }
}
public class Child
{
public virtual IParent Parent { get; set; }
}
The question now is how would you map such a relationship in NHibernate?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Ayende 有一个示例 NHibernate 博客模型。
Tag
实体通过名为Entity
的属性对其父级进行引用。Entity
的类型是object
,它被映射为Post
或Blog
。它可能就是您正在寻找的。模型中的所有文件此处
标签 源 和 映射
Ayende has a sample NHibernate Blog model. And
Tag
entity has a reference to its parent with property calledEntity
. Type of theEntity
isobject
, and it is mapped either to be aPost
or aBlog
. Its might be what you are looking for.All files in the model here
Tag source and mapping