views:

222

answers:

2

Hi,

I have a product that has 1 or more product relations.

Entities: Product and ProductRelation

So product has a property List(Of ProductRelation)

Now I have a checkboxlist where I can select a number of products that I want to assign to this product.

When I add a new collection of ProductRelations with the new products, It should delete all old relations and save the new one. But this does not work. It does not delete the old one and also not saves the new one.

I have used the following hbm.xml

<bag name="RelatedProduct" inverse="true" lazy="true" cascade="all">
  <key column="FromID" />
  <one-to-many class="Kiwa.Objects.RelatedProduct,Kiwa.Objects" />
</bag>
+1  A: 

Your hbm file is not visible. :)

But, why do you add a new collection ?
This is the reason why things are going wrong. You should clear the collection (remove the items from the collection), and just add the new items to the collection, without replacing the collection itself.

Frederik Gheysels
A: 

You should NEVER replace mapped collection once it has been persisted. NHibernate needs that specific collection instance (which is created / injected by NHibernate during entity load) to track deletes.

You should instead delete / update / replace individual elements (e.g. RelatedProduct instances) within existing collection. If you truly want to delete all the previously saved RelatedProducts and insert new ones (why?), you can clear the RelatedProduct List - but don't replace it with a new List instance.

ChssPly76
Easiest way to enforce this is to declare your collection property as protected set, so you're limited in replacing it.
Simon Svensson