.netnhibernateormisession

How to Queue The Add Operation in NHibernate?


Is there some way to queue the "Add" operation in NHibernate, just like Linq to SQL and Entity Framework does?

I want to add an entity to the repository, but I don't want to save it to the database immediately, because the business logic is complex. I want to submit all changes when I call ITransaction.Commit(), just like Entity Framework does. Can I do that?

NOTE: I found that NHibernate will execute an insert command as soon as I call ISession.Save() method, that's not expected.

UPDATE: I try setting the FlushMode to Commit. But it still save the entity immediately, not commit all changes when I call ITransaction.Commit().

UPDATE 2 I found the reason here http://nhibernate.info/doc/nh/en/index.html#manipulatingdata-flushing : (An exception is that objects using native ID generation are inserted when they are saved.). So I can only change the Id generator to some thing other than "identity"? No other solutions similar to the solution in EntityFramework?


Solution

  • That's how identity works; session.Save returns the POID and the only way to get it when using identity is performing the actual insert.

    More about that here: http://fabiomaulo.blogspot.com/2009/02/nh210-generators-behavior-explained.html