Reputation: 3515
My domain object Page has properies
public class Page
{
public virtual string Title {get; set;}
public virtual Page Parent {get; set;}
public virtual IList<Page> ChildPages {get; set;}
}
I was thinking to map object to the db like this
Bag(x => x.ChildPages,
b =>
{
b.Inverse(true);
b.Cascade(Cascade.DeleteOrphans);
},
r => { r.OneToMany(); }
);
ManyToOne(x => x.ParentPage, m =>
{
m.Cascade(Cascade.All);
}
);
To make it clearer please look this from this perspective Page is page object in context of web page. That page can have 0 or many child pages and one page can belong to 0 or 1 parent page.
So is this mapping ok, I'm especially anxious about cascade and inverse attributes.
Thanks
Upvotes: 0
Views: 125
Reputation: 13410
That is a parent child relationship you want to map. There are several posts about the different kind of parent child and tree structures out there if you just use google.
In general it actually depends on what you want to achieve when it comes to persisting your entity and in which direction (up down) in the tree you want to save the objects...
Inverse you tell nhibernate which side of the relationship is the owner. If you set inverse= true, it means that this is not the owner. When you mark the collection end as 'inverse', then NHibernate will first persist the entity that 'owns' the collection, and will persist the entities that are in the collection afterwards, avoiding an additional UPDATE statement.
To correctly persist your object as you want it, you have to play around a little bit with the cascading. You can also tell the map when to delete or update an entity only...
If you're new to nHibernate, I found doing mappings with Fluent a lot easier to get used to, and there are lot more examples for fluent mappings for this kind of stuff.
Upvotes: 1