开发者

Nhibernate delete object functionality coupled with database trigger throw StaleStateException

The design I'll describe violates somehow good practices design but still I'm interested in why this issue appears and how it can be bypassed. I found some description of problems that can occur using NHibernate and Update or Select triggers however I could find nowhere the problem I face.

I have a table and class mapped to it called Bucket:

public class Bucket
{
  public virtual int Id;
  public virtual int OrderCount;
}

      <class name="Bucket" table="Bucket">
        <id name="Id">
          <generator class="native" />
        </id>
        <property name="OrderCount" />

and class Order

public class Order
{
  public virtual int Id{get;set;}
  public virtual int BucketId{get;set;}
}


<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
                   assembly="..."
                   namespace="...

  <class name="Order" table="Order">
    <id name="Id">
      <generator class="native" />
    </id>
    <property name="BucketId" />
</hibernate-mapping>

There are also two triggers that counts the Order by certain bucket id at each insert or delete respectively and update OrderCount in the Bucket table for certain id of the bucket.

The problem is that during test runs i开发者_StackOverflowf I delete the Order and flush the session an exception with the following message is thrown: NHibernate.AdoNet.TooManyRowsAffectedException: Unexpected row count: 2; expected: 1

that is probably caused by trigger execution right after Order is deleted.

PS: Trigger is counting Order records and updates Bucket record in Read Commited isolation level.


If you're using SQL Server, SET NOCOUNT ON in the trigger will do the trick.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜