NHibernate Tour (21): Exploring Object Status

Source: Internet
Author: User

The contents of this section

Introduced

Object state

Object state Transitions

Conclusion

Introduced

Manipulating the database using objects in the process of running the program inevitably results in a series of instance objects of the persisted class. These objects may be just created and ready to be stored, or may be queried from the database, in order to differentiate between these objects, depending on the state of the object and the current session, we can divide the object into three categories:

Instantaneous object: Object just established. The object has no records in the database and is not in the ISession cache. If the object is an auto-generated primary key, the object's object identifier is empty.

Persisted object: The object has been persisted through nhibernate, and the corresponding record already exists in the database. If the object is an automatically generated primary key, the object identifier for that object has been assigned.

Managed Object: The object was saved by NHibernate or fetched from the database, but the isession associated with it has been closed. Although it has an object identifier and a corresponding record in the database, it is no longer managed by NHibernate.

Object state

NHibernate provides the function of object state management, supports three kinds of object states: Instantaneous state (Transient), persistent State (persistent), managed State (Detached).

1. Instantaneous state (Transient)

The object has just been created and has not come to the state associated with the ISession. The instantaneous object is not persisted to the database and is not assigned an identifier. If not used, it is destroyed by GC. The ISession interface can convert it to a persistent state.

This is like this, just created a customer object, is a transient state object:

var customer = new Customer() { Firstname = "YJing", Lastname = "Lee" };2. Persistent State (persistent)

Just saved or just loaded from the database. The object is valid only in the associated ISession lifecycle and has a corresponding record in the database with an identifier. Object instances are managed by the NHibernate framework, and if any changes are made, synchronize with the database when the operation is committed, and update the object to the database.

3. Managed state (Detached)

After the isession of the persistent object is closed, the object is detached from the relationship in the ISession, is the managed state, the managed object still has all the properties of the persisted object, the reference to the managed object is still valid, and we can continue to modify it. If you reconnect this object to the ISession, it is again converted to a persistent state, and the modifications in the managed period are persisted to the database.

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.