0

I'm having a problem, and just in some classes, most of then work, and I can't find why other don't. A can create new objects and save without any problem, but in this particular case I can't save a change

I have a parent abstract class:

[DataContract]
public abstract class BaseClass<T> where T : Object
{
    public BaseClass(){}

    [DataMember]
    public virtual int Id{ get; set; }

    [DataMember]
    public virtual string Code { get; set; }
}

And my class, with problems:

public class NewClass: BaseClass<OtherClass>
{
    public NewClass(){}
}

If I create an NewClass object and save it works fine:

var newClass = new NewClass{Code="1"};
Session.SaveOrUpdate(newClass);

Now if I change the Code property value and save, no update is executed in the database.

var newClass = Session.Load<NewClass>(id);
newClass.Code = '01';
Session.SaveOrUpdate(newClass);

EDIT: If I put a transaction, and commit the change, it still do not work.

2
  • Please show us the example where it's in the transaction. Also, are you absolutely sure the old "Code" value is different from the new "Code" value. Commented Feb 5, 2013 at 4:03
  • Sorry for the delay. The "Code" values are different, I'll try to make a full example and update the question. Commented Mar 25, 2013 at 16:57

1 Answer 1

1

This is normal behaviour. NH flushes the updates when necessary. SaveOrUpdate is completely unnecessary in the last snippet. It is only required to put new or detached objects into the session. After an object is in the session, NH decides when it is stored to the database. This should all be transparent to your code.

NH Flushes:

  • when committing
  • when calling session.Flush()
  • Before queries (to execute queries on actual data)
Sign up to request clarification or add additional context in comments.

4 Comments

Even if a start a transaction and commit, it is not updated. Id I get the object back from database with Session.Load it is not updated, if I check the table with sql, it is still not updated.
NH doesn't really "decide" anything. If I am not mistaken, NH flushes the session when the transaction is committed by default.
What do you mean: "SaveOrUpdate is completely unnecessary in the last snippet" ? Every SaveOrUpdate should be followed by Flush ? That seems weird.
Everything that is in the session is automatically synchronized with the database. Flush is done by NH, no code in the business logic is required. This is called flush mode "auto flush". It can be disabled. Business logic consists of: load from db, change properties, commit. That's all. No save, update, flush required.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.