Nhibernate--save Method:
CustomerService.cs
1 Public voidSave (Customer customer)2 {3ISession session =_sessionmanager.getsession ();4ITransaction transaction =session. BeginTransaction ();5 6 Try7 {8 session. Save (customer);9 transaction.commit ();Ten } One Catch(Exception) A { - transaction. Rollback (); - Throw; the } - finally - { - session. Close (); + } -}
View Code
After you add the entity object, modify the value, call the Save method, and the modified entity object is saved as the new entity object to the database. Therefore, it is recommended to call the session's Saveorupadate or Update method
As shown in the following:
Again, whether the entity object from the table will also be added repeatedly as a new entity:
Tests show that:
1. The modified entity object of the primary table shows that the call to save will be added to the database when the new entity object.
2. From the table configuration unsaved-value= "0": NHibernate automatically takes the ID from the table entity to compare with Unsaved-value, automatically decides to call save or update.
The entity object from the table is not added repeatedly as a new entity
3. Interestingly, the main table entity associated with the entity object of the table has changed. Originally associated with the customerid=77,
Modified to call the Main Table object again customer (original customerid=77, modified to customerid=78,) to display the call to the Save method from the entity object of the table Associated with the customerid=78,
The third Test, which commented on the configuration of the table unsaved-value= "0", whether the entity object from the table will also be added repeatedly as a new entity
such as testing:
It has been tested that:
Note the configuration of the table unsaved-value= "0"
The entity object from the table is not added repeatedly as a new entity
This shows me this article:
01-03-01 "NHibernate in and out of the lake" ID tag Unsaved-value property
is a bit of a deceptive ingredient, but it is best to add it, there may be a version of the problem and compatibility with the old version (my nhibernate version is Nhibernate-3.3.3.cr1-bin).
Or that sentence: it is better to believe in it, not to believe it.
Conclusion:
With this many tests, the session's Save method is to add a new entity to the TMD. Does not contain any updates, you will know to create a new.
And there you are. New to add, but also the original entity of the sub-table objects together to take away, you what the hell you want to do.
I don't know if save has a fart. My Grass!!!!!!!!!
It is recommended to call the session's Saveorupadate or Update method