views:

23

answers:

2

Hi guys,

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://nhforge.org/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?

+3  A: 

Use session.BeginTransaction() and read about transactions.

jgauffin
I mean, NHibernate issues an INSERT sql command before I call transaction.Commit(), that's not what I want. I want it to issues sql commands only when I call transaction.Commit().
Dylan Lin
+1  A: 

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

Diego Mijelshon