. NET advanced-three States of nhib.pdf persistence

Source: Internet
Author: User

. NET advanced-three States of nhib.pdf persistence
Introduction

In object-oriented development, programs run by adding, deleting, modifying, and querying databases through objects, and some column objects, such as login, must be generated during the persistence process, there must be instantiated UserEntity objects, Select objects from the database, Save objects to the database, how to distinguish these objects and the status of the current session. The following describes the object state management in nhib.pdf:


Nhibernate three-state


In the lifecycle of a persistent object, Nhibernate divides the object into three states:

1. Transient)

It is also called a temporary state. As the name implies, an object is created temporarily, that is, an object just instantiated:

UserEntityenUser = new UserEntity ();

This enUser object is now in the Instantaneous State, because it only allocates a piece of memory space to the enUser object, and has not yet entered the Session cache of Nhibernate, nor persisted to the database.

Instantaneous object features:

  The object does not have a record in the database or conflict with each other.Session Association.

2. Persistent)

Objects saved to the database or queried from the database, and are still in Session management.

Converting an instantaneous object to a persistent object: (1) using the Save () and SaveOrUpdate () Methods of the Session to persists the instantaneous object to the database; (2) using Get (), Load () and other query methods to query data objects.

Persistence object features:

  The object is synchronized with the records in the database, and the data changes are inUnder the management of the Session.

3. Detached)

After the Session associated with the persistent object is closed, the object becomes free, but it also has all attributes of the Persistent Object.

Features of the Free object:

  Like an instantaneous objectSession Association. The difference is that the free object is a persistent object, which is recorded in the database.

Switching between three States

The above briefly introduces three states of Nhibernate persistence. in the life cycle of persistence objects, these three states are not isolated, and there is a process of mutual conversion between them:

Status Analysis:

<喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> Authorization + cjxwp1_vcd4kpha + Ly/LssqxzKy21M/zPC9wPgo8cCBsYW5nPQ = "en-US"> UserEntity enUser = new UserEntity ();

EnUser. name = "yiming ";

// Associate Session, call Save (enUser) to persist to the database, and the status changes to persistent state, but it has not been updated to the database. You need to perform flush ().

Session. Save (enUser );

EnUser. name = "liuming ";

Session. Save (enUser); // invalid

// Synchronize the data in the session cache with the database and disable the Session

Session. Flush ();

Note: During the persistence operation, the persistence operation is not immediately executed (updated to the database), but recorded until the Session. it is easy to understand the reason why the database is actually updated when it is flushed to avoid frequent database connection operations. If the session is closed without calling Flush, the persistent objects in the current session will not be persistent! It makes no sense to call Save () or Update () for a persistent object.

2. Transition from persistent state to free state

Converting a persistent state to a free state is a process of clearing the Session. The methods listed in the figures evict (), close (), and clear () are not called manually, but managed by Nhibernate, cleaning time:

1. When the commit () method is called.

2. During the query, the cache is cleared by commit to ensure that the query results reflect the latest status of the object.

3. Call the session. flush () method.

3. Transition from free to persistent

Take the update () method as an example:

Session. Update (enUser );

If the enUser is a persistent object, it can be synchronized to the database without any operation. If the enUser is a free object, it must first be converted to a persistent object and then synchronized to the database. Therefore, the select statement is also issued during Update.

Other methods are not described one by one. As long as you understand these three states and the conversion process, it is easy to understand it based on our previous experience in implementing SQL statements (because these methods of nhib.pdf are still used to operate databases through SQL statements.

Summary

During one month of development, the most common problems encountered in the complaint system are the problems encountered in calling the underlying Save, Update, SaveOrUpdate methods, and cascade operations, due to lack of understanding about these three states, errors are constantly being made. It is a try of a method and a method, and some rules have been found, in addition, this study makes the three states of Nhibernate clearer.


This article also keeps showing the word Session, which is no stranger. But is the Session of Nhibernate used before? What is the role of the Session in Nhibernate, enter the next article with questions (nhib.pdf cache mechanism)



Related Article

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.