01-03-02-2 "Nhibernate (version 3.3.1.4000) in and out of the lake" Some problems of Crup operation-save Method

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.