views:

63

answers:

1

I am trying to optimize a tree-structure for my category-model. The Category-model has a Parent-property and a Children-collection.

The way i normally do this, is to load all categories (sounds bad, but max 100 nodes). The tree is then manually assembled, by indexing all categories by id, and then by looking up the parent by the categories parentid. Dirty but really quick. The problem i have, is that i dont know how to get/map the ParentID from the parent-relation when using nhibernate.

Say i have this mapping in fluent nhibernate:

        References(cat => cat.Parent,"Parent_id")
            .FetchType.Select()
            .WithForeignKey("Category_ParentCategory");

My question is then: How can i get or map the parentid on a given category, without loading the Parent from the database?

And also, has anyone a practical experience in mapping trees, or tried http://blogs.hibernatingrhinos.com/nhibernate/archive/2008/05/14/how-to-map-a-tree-in-nhibernate.aspx?

A: 

You can map the ParentId as a property in addition to mapping the Parent object. You will probably want to map ParentId as read only and put logic in the setter for Parent to set ParentId.

However, this may not be necessary. If you're populating the tree in a single ISession then each Parent will only be loaded from the database once and retrieved from cache on subsequent requests.

Jamie Ide