tags:

views:

26

answers:

1

Looking for some advice on how to update a collection on an entity. In a web app - we have a multiselect listbox allowing a user to assign and remove child entities from a list of available entities. The user would select the relevant children to associate with the parent. i.e. Product with multiple Categories it could belong to. Once the user is satisfied, they submit and we update the entities.

What is the preferred way to update(delete removed children, add new children) to the collection taking performance into account. I would not want to run several sql statements to fetch each child and add it to the parent.

Cheers

Mappings Attached:

 public class ParentMap : EntityMapping<Parent>
    {
        public ParentMap()
        {
            Map(x => x.Name);
            HasMany(x => x.Children)
                .Cascade.AllDeleteOrphan()
                .Access.LowerCaseField(Prefix.Underscore);
        }
    }

    public class ChildMap : EntityMapping<Child>
    {
        public ChildMap()
        {
            References(x => x.Parent);
        }
    }

 public abstract class EntityMapping<TEntity> : ClassMap<TEntity> where TEntity : EntityBase
    {
        protected EntityMapping()
        {
            Id(x => x.Id, "Id")
                .UnsavedValue("00000000-0000-0000-0000-000000000000")
                .GeneratedBy.GuidComb();
            OptimisticLock.Version();
            Version(entity => entity.Version);
        }
    }
A: 

Establish a cascade relation between parent and child entities and force it to act on all operations like update, delete, etc. You must define cascade behavior in you HBM mapping files. For more info: http://nhforge.org/doc/nh/en/index.html#mapping-declaration-mapping

afsharm
I have cascade all delete orphan selected and it does not delete ever - only add.
Chev