views:

82

answers:

1

I have an NHibernate entity which contains a many-to-many list of related items. When a certain property of the entity changes, a database trigger is called that removes all associations between the entity and any entities that it is joined to. However, NHibernate throws a StaleStateException with a message along the lines of:

Batch update returned unexpected row count from update; actual row count: 0; expected: N

This presents a problem because it causes a rollback of the transaction and the entity is not saved. This arises because when the entity is updated, all of the entities in the many-to-many collection are also updated by NHibernate, but they no longer exist because of the trigger, which then causes the updates to fail giving back a row count of 0.

I unfortunately cannot change this database behavior and implement this entirely in NHibernate.

Is there anyway to prevent this behavior or update the entity without updating the entities in the many-to-many association?

A: 

Use set nocount on in the trigger

Diego Mijelshon
This is not the problem. This occurs after the trigger executed and NHibernate is attempting to update the deleted entries. The deleted entries no longer exist, so 0 rows are affected which causes NHibernate to throw an exception because it expected N rows (the rows deleted by the trigger when the parent entity was updated) and not 0 rows.
Joshua Rodgers
Have you actually tried what I said before downvoting? Also, if you have DB cascades (even if implemented by a trigger), you should tell NH by means of the on-delete attribute.
Diego Mijelshon
I have tried what you said. In fact the trigger had "SET NOCOUNT ON" from the start. Please do not assume I don't know what I'm doing. You obviously did not thoroughly read my question before giving an answer that doesn't work.
Joshua Rodgers
Actually, if you use triggers instead of ON DELETE CASCADE, I *do* assume you don't know what you're doing. More so if you don't post the code of the triggers, nor the classes, nor the mappings, expecting a magical answer to a poorly-defined problem.
Diego Mijelshon