17

I'm having difficulty to figure out NH exception:

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

My Dal code:

 public T Save(T item)
        {
            using (ISession session = NHibernateHelper.GetSession())
            {
                using (var transaction = session.BeginTransaction())
                {
                    session.SaveOrUpdate(item);

                    transaction.Commit();

                    return item;
                }
            }
        }

My log4net file:

command 0:UPDATE Metadata_FriendDetails SET UserId = @p0, Email = @p1, Name = @p2 WHERE FriendId = @p3;@p0 = a358ab9d-d481-4bdd-8cb2-18ddc8898c70 [Type: Guid (0)], @p1 = '[email protected]' [Type: String (4000)], @p2 = 'myFriend' [Type: String (4000)], @p3 = 2ea35658-edb1-43e3-b848-e1915d1042df [Type: Guid (0)]

2011-12-02 13:06:49,750 [Agent: adapter run thread for test 'SaveFriendDetails_Test' with id '17016e48-1ff4-4dea-8536-6aa3b24e5fb7'] DEBUG NHibernate.AdoNet.AbstractBatcher - Closed IDbCommand, open IDbCommands: 0 2011-12-02 13:06:49,751 [Agent: adapter run thread for test 'SaveFriendDetails_Test' with id '17016e48-1ff4-4dea-8536-6aa3b24e5fb7'] ERROR NHibernate.Event.Default.AbstractFlushingEventListener - Could not synchronize database state with session NHibernate.StaleStateException: Batch update returned unexpected row count from update; actual row count: 0; expected: 1

Does anyone see the problem?

TIA

3
  • I think you should write the source of exception with the exception. It will be more helpfull.
    – Joshi
    Dec 2, 2011 at 4:31
  • 2
    A couple of possiblites, your object is detached and was never actaully created in the databse, so there is no orginal object row to update. Or something has changed a value in the table before your update. I presume that this is a repeatible exeception ? Posting some code snipts on how you are creating, updating, and saving the object would be helpful.
    – user957902
    Dec 2, 2011 at 4:32
  • added.i cannot see why: NHibernate.Event.Default.AbstractFlushingEventListener - Could not synchronize database state with session NHibernate Dec 2, 2011 at 11:15

2 Answers 2

33

the problem was that on Save I shouldn't supply the id value and on update I must supply it.

1
  • 1
    As soon as you are able, be sure and mark this answer as accepted so others will know there is a solution. Dec 2, 2011 at 22:26
3

You must set the Id object to null before saving.

example:

obj.setId(null);
objectDao.save(obj);
1
  • 1
    if you have an Integer ID you have to use: obj.setId(0) - so it works. Wasn't working for me, because I had set the Id to -1 Mar 11, 2015 at 10:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.