nhibernate mapping: delete collection, insert new collection with old IDs

Posted by npeBeg on Stack Overflow See other posts from Stack Overflow or by npeBeg
Published on 2010-03-31T12:33:19Z Indexed on 2010/03/31 13:33 UTC
Read the original article Hit count: 235

Filed under:
|
|

my issue lokks similar to this one: (link)

but i have one-to-many association:

<set name="Fields" cascade="all-delete-orphan" lazy="false" inverse="true">
  <key column="[TEMPLATE_ID]"></key>
  <one-to-many class="MyNamespace.Field, MyLibrary"/>
</set>

(i also tried to use ) this mapping is for Template object. this one and the Field object has their ID generators set to identity.

so when i call session.Update for the Template object it works fine, well, almost: if the Field object has an Id number, UPDATE sql request is called, if the Id is 0, the INSERT is performed. But if i delete a Field object from the collection it has no effect for the Database. I found that if i also call session.Delete for this Field object, everything will be ok, but due to client-server architecture i don't know what to delete.

so i decided to delete all the collection elements from the DB and call session.Update with a new collection. and i've got an issue: nhibernate performs the UPDATE operation for the Field objects that has non-zero Id, but they are removed from DB!

maybe i should use some other Id generator or smth.. what is the best way to make nhibernate perform "delete all"/"insert all" routine for the collection?

© Stack Overflow or respective owner

Related posts about nhibernate

Related posts about collection