Adrian Poplavsky
Adrian Poplavsky

Reputation: 136

Nhibernate - item gets its ParentID updated to null instead of being deleted

The business logic inside a process is:

Im using cascade all-delete-orphans, and the inverse=true, both in my parent class. When removing the item from the collection, I set the .parentObj = null and remove the item from the collection.

When using TemplateFlushMode.Auto, I profiled the Database and the following is done:

(the insert item is done because a find() is done, to guarantee data consistency in the database). The the select is done, and then I would expect a DELETE to be performed... but an update to parentID = null is done.

When performing a Session.Flush() right before the Find(), the following happens:

When using TemplateFlushMode.Commit, the changes are done at the end of the transaction and the item is never inserted:

The app I'm working with is using TemplateFlushMode.Auto, so I'm wondering, ,is there an explanation why the cascading is not working if a select is done in between one item is added and then removed in the same transaction?

I know the first answer that comes up is "don't add an item to a collection, if it will be removed afterwards". But I rather don't change the business logic.

Does anyone know why an update is being done instead of a delete when using AUTO? (when using Commit it does not insert the object)

Upvotes: 8

Views: 330

Answers (2)

Helikaon
Helikaon

Reputation: 1508

I guess you should use the cascade="delete" option in your mapping file in order to cascade on delete

Upvotes: 1

AYK
AYK

Reputation: 3322

Are you using a two way one to many binding ? Have you marked not-null="true" on the foreign key in the details table ? Can you please post your mappings XML file for better understanding ?

I know that i haven't answered your question, but I do not have enough reputation to post a comment instead.

Upvotes: 0

Related Questions