NHibernate ISession.Update

后端 未结 2 1437
庸人自扰
庸人自扰 2021-01-20 05:22

I have noticed, by using log4net, that when calling ISession.Update, it updates all the changed objects.
For example:


// Change 2 instances 
user1.IsD         


        
相关标签:
2条回答
  • 2021-01-20 06:03

    This is the default behavior when FlushMode of session is Auto or Commit.

    In these cases calling transaction.Commit() flushes the session & updates ALL persistent objects

    So if you remove the calls Session.Update it wouldn't make any difference

    Can I make NHibernate update one by one?

    Yes. use FlushMode.Never or postpone commiting the session if possible. I guess you don't need to use Evict for this case

    0 讨论(0)
  • 2021-01-20 06:26

    It's the normal and default behavior:

    Hibernate maintains a cache of Objects that have been inserted, updated or deleted. It also maintains a cache of Objects that have been queried from the database. These Objects are referred to as persistent Objects as long as the EntityManager that was used to fetch them is still active. What this means is that any changes to these Objects within the bounds of a transaction are automatically persisted when the transaction is committed. These updates are implicit within the boundary of the transaction and you don’t have to explicitly call any method to persist the values.

    From Hibernate Pitfalls part 2:

    Q) Do I still have to do Save and Update inside transactions?

    Save() is only needed for objects that are not persistent (such as new objects). You can use Update to bring an object that has been evicted back into a session.

    From NHibernate's automatic (dirty checking) update behaviour:

    I've just discovered that if I get an object from an NHibernate session and change a property on object, NHibernate will automatically update the object on commit without me calling Session.Update(myObj)!

    Answer: You can set Session.FlushMode to FlushMode.Never. This will make your operations explicit ie: on tx.Commit() or session.Flush(). Of course this will still update the database upon commit/flush. If you do not want this behavior, then call session.Evict(yourObj) and it will then become transient and NHibernate will not issue any db commands for it.

    0 讨论(0)
提交回复
热议问题