views:

31

answers:

1

In a project I am working on, we are using tabs. These tabs have content. Multiple content objects can be on a tab. So we could have a 'car' tab, and that car tab may display a sedan content object, a suv content object and a truck content object. The user could also specify that it contains more or less of these objects and order them however they want. It is important for us to maintain the order of these content objects to user specification.

Here are my mappings :

On the Tab mapping :

    HasManyToMany(t => t.ContentObjects) 
        .Table("TabContentObjects") 
        .ParentKeyColumn("TabID") 
        .ChildKeyColumn("ContentObjectID") 
        .AsList(index => index.Column("SortOrder")); 

On the ContentObject mapping :

    HasManyToMany(c => c.Tabs) 
        .Table("TabContentObjects") 
        .ParentKeyColumn("ContentObjectID") 
        .ChildKeyColumn("TabID") 
        .Cascade.SaveUpdate() 
        .AsList(index => index.Column("SortOrder")); 

The association table is :

TabContentObjectId TabId ContentObjectId SortOrder int not null

I am able to add a content object, re-order a content object within a tab, and all is well. nHibernate is adding / updating the SortOrder appropriately. The problem comes when I am trying to delete a content object. When I go to delete the tabs from this contentObject, contentObject.Tabs looks odd.

Here is the code I use to delete the tabs :

            //Need to remove each through the contentObject since it is parent 
            foreach (Tab tab in deleteContentObject.Tabs) 
            { 
                tab.RemoveContentObject(deleteContentObject); 
            } 

//in Tab class 
public virtual void RemoveContentObject(TabContentObject item) 
{ 
    if (ContentObjects == null) ContentObjects = new List<TabContentObject>(); 
    ContentObjects.Remove(item); 
} 

If it is the ONLY, or the first (SortOrder = 0) contentObject within a tab, I can delete. If it is the second content object, contentObject.Tabs look like [0] [null], [1] [Tab]. If it is the fourth contentObject on the tab, contentObject.Tabs looks like [0] [null], [1] [null], [2] [null], [3][Tab]. So, depending on what the SortOrder column is in the association table, I seem to have many null references returned, and therefore cannot delete due to a null reference. I cannot figure out why these nulls are being returned. Any help would be appreciated.

A: 

I have resolved this with the following :

Tab Map :

        HasManyToMany(t => t.ContentObjects)
            .Table("wfdc_Tab_TabContentObjects")
            .ParentKeyColumn("TabID")
            .ChildKeyColumn("ContentObjectID")
            .Cascade.SaveUpdate()
            .AsList(index => index.Column("SortOrder"));  

Content Object Map :

        HasManyToMany(c => c.Tabs)
            .Table("wfdc_Tab_TabContentObjects")
            .ParentKeyColumn("ContentObjectID")
            .ChildKeyColumn("TabID")
            .Inverse();

So I only needed the AsList on one map. I also made the Tab the parent.

To remove the tabs :

                foreach (Tab tab in deleteContentObject.Tabs)
                {
                    tab.ContentObjects.Remove(deleteContentObject);
                }

With this configuration, a tab's content is ordered the way we would like. Sorting is automatically updated by nHibernate on updates, add, deletes.