DELETE then INSERT in collection - Order of executed SQL

32 views
Skip to first unread message

chetan chaphekar

unread,
Dec 6, 2011, 8:37:14 AM12/6/11
to nhu...@googlegroups.com
Hi,
 
I have two objects Client & Contact Person. Client object has collection of contact persons. When Client object is saved in update mode, it throws an error message ' Unique Key Violation',
Contact Person has a field CPCode which has Unique key constraint in Ms Sql. When i traced through sql profiler, I came to know that nhibernate fires Insert Statements first and then delete statements, because of this sql throws  ' Unique Key Violation' error.
 
Now my question is how can i change the sequence, i want nhibernate to fire delete statement first and then insert.
 
Please help
 
Regards
Chetan Chaphekar

Oskar Berggren

unread,
Dec 7, 2011, 10:56:45 AM12/7/11
to nhu...@googlegroups.com
2011/12/6 chetan chaphekar <cchap...@gmail.com>:


One thing you can do is call Flush() on the ISession, or otherwise
trigger a flush after removal of the first element and before addition
of the new element. Not the most beautiful code though.

/Oskar

Jason Meckley

unread,
Dec 7, 2011, 12:29:08 PM12/7/11
to nhu...@googlegroups.com
why not update the record rather than delete and insert?

Ramon Smits

unread,
Dec 7, 2011, 12:50:27 PM12/7/11
to nhu...@googlegroups.com

Well that is probably his goal but code wise not really efficient. I often have the same problem which I usually solve by performing a .Clear() and then adding the items by creating new items based on a collection of dto items. These items are 99% of the time the exact same items value wise. Adding add/update/delete logic often is a shitload of code thus increased development time.

On Dec 7, 2011 6:29 PM, "Jason Meckley" <jasonm...@gmail.com> wrote:
why not update the record rather than delete and insert?

--
You received this message because you are subscribed to the Google Groups "nhusers" group.
To view this discussion on the web visit https://groups.google.com/d/msg/nhusers/-/81zueJdpjhkJ.
To post to this group, send email to nhu...@googlegroups.com.
To unsubscribe from this group, send email to nhusers+u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/nhusers?hl=en.

Maximilian Raditya

unread,
Dec 7, 2011, 12:58:31 PM12/7/11
to nhu...@googlegroups.com
On Tue, Dec 6, 2011 at 8:37 PM, chetan chaphekar <cchap...@gmail.com> wrote:
...
Now my question is how can i change the sequence, i want nhibernate to fire delete statement first and then insert.
...
 
 
I'd rather see the mapping and code that lead to such behavior.
 
 
 


 
Chetan Chaphekar

--
You received this message because you are subscribed to the Google Groups "nhusers" group.
To post to this group, send email to nhu...@googlegroups.com.
To unsubscribe from this group, send email to nhusers+u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/nhusers?hl=en.



--
Regards,

Maximilian Haru Raditya

TheCPUWizard

unread,
Dec 7, 2011, 1:48:35 PM12/7/11
to nhu...@googlegroups.com

Delete then Insert can lead to it’s own set of problems. From a “pure” standpoint, the deletes and inserts need to be sorted by time of occurrence, optimized to remove redundant operations, and then batched and fired.

 

I do not know of any way to do this with nHibernate, but there are some libraries that will do this directly at the ADO.Net level.

chetan chaphekar

unread,
Dec 8, 2011, 9:57:20 AM12/8/11
to nhu...@googlegroups.com
Hi,
 
I think there is no easy way to do this. The only solution to this which mr Oskar has described, to Flush the session after removal of element before addition. but i still dont know why nhibernate has this behavour.
 
Thanks all of you for your help.
 
Regards
Chetan Chaphekar

Oskar Berggren

unread,
Dec 8, 2011, 10:13:49 AM12/8/11
to nhu...@googlegroups.com
On the other hand, you speak of collections. According to the
reference, collection deletions should run before insertions.
http://nhforge.org/doc/nh/en/index.html#manipulatingdata-flushing

/Oskar


2011/12/8 chetan chaphekar <cchap...@gmail.com>:

Reply all
Reply to author
Forward
0 new messages