NHibernate map single table with parent / child relationship
I am experimenting with NHibernate instead of writing all my own SQL. But I already run into trouble mapping my experimental Domain model of just a few classes. The problem I am currently having is the relationships as demonstrated by the class below.
Can anyone show my how this would be mapped in NHibernate? (The XML)!
public class Category
{
public virtual Guid Id { get; set; }
public virtual string Description { get; set; }
public virtual Category Parent { get; set; }
public virtual ISe开发者_如何学Got<Category> Subcategories { get; set; }
}
Since the link in the accepted answer is dead here is my answer.
You can map the relation to the parent just like any other many to one relation.
<many-to-one name="Parent" class="Category" column="ParentId(Put the correct name of the column here)"/>
I'm also quite new to NHibernate. I'd suggest looking at the documentation, because there are many things with NHibernate which you should understand before writing production code.
Take a look at quick starter guide
it shows how to install nhibernate and it's xml mapping xsd to have intellisense in Visual Studio. It also shows how to do a simple mapping that you want. Also here's the in depth manual
Trust me, it's always better to come to the correct solution by yourself than someone giving you a ready to go answer. at least, while you are learning a new technology.
Good luck and let us know if you'll have any other questions
P.S. as a starter, I'd recommend downloading an evaluation version of NHibernate Profiler, it helped me a lot.
精彩评论