Nhibernate - the element gets its ParentID updated to zero, and not deleted

Business logic inside the process:

  • start a transaction
  • add item to collection
  • do a search ("somethingA")
  • delete this item depending on the previous step.
  • make a transaction

Im uses cascading syntaxes all-delete-andphans and inverse = true, as in my parent class. When removing an item from the collection, I set .parentObj = null and remove the item from the collection.

When using TemplateFlushMode.Auto I profiled the database and did the following:

  • INSERT element
  • SELECT related to tosomethingA
  • UPDATE parentID (FK for parent) of an element in NULL

(the insertion element is executed as find() is executed to ensure data consistency in the database). The choice is made, and then I would expect DELETE to complete ... but the update is being done to parentID = null .

When executing Session.Flush() before find() , the following happens:

  • INSERT element
  • SELECT related to tosomethingA
  • DELETE Item

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

  • SELECT related to tosomethingA

The application I'm working with uses TemplateFlushMode.Auto , so I'm wondering if there is an explanation why cascading does not work if the choice is made between one element and then deleted in the same transaction?

I know that the first answer that appears is "don't add the item to the collection if it is deleted after." But I probably will not change the business logic.

Does anyone know why updating is performed instead of DELETE when using AUTO ? (when using Commit it does not insert an object)

+8
c # nhibernate nhibernate-mapping
source share
2 answers

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

+1
source share

Do you use two-way binding? Did you mark not-null="true" for the foreign key in the details table? Can you host an XML mapping file for better understanding?

I know that I did not answer your question, but I do not have enough reputation to post a comment.

0
source share

All Articles