Ron
Ron

Reputation: 245

How to map multiple root types for aggregate relations in 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?

Upvotes: 2

Views: 344

Answers (1)

Sly
Sly

Reputation: 15227

Ayende has a sample NHibernate Blog model. And Tag entity has a reference to its parent with property called Entity. Type of the Entity is object, and it is mapped either to be a Post or a Blog. Its might be what you are looking for.

All files in the model here Tag source and mapping

Upvotes: 1

Related Questions