The following are information of unknown origin:
Introduced
Manipulating the database in the same way that the program is run will inevitably result in a series of persisted class instance objects. These objects may have just been created and ready to be stored, or they 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 kinds:
Instantaneous object: Object just established. The object is not logged in the database and is not in the ISession cache. If the object is automatically generated by the primary key, the object identifier for the object is empty.
Persisted object: The object has been persisted through nhibernate, and the corresponding record already exists in the database. If the object is automatically generated by the primary key, the object identifier for the object has been assigned a value.
Managed Object: The object was nhibernate saved or fetched from the database, but the isession associated with it has been closed. Although it has an object identifier and a corresponding record exists 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: Transient state (Transient), persistent State (persistent), and managed State (Detached).
1. Transient state (Transient)
The object has just been created and has not come in the state associated with the ISession. Instantaneous objects are not persisted to the database and are not assigned identifiers. If not used, it is destroyed by GC. The ISession interface can convert it to a persistent state.
This, like this, has just created a customer object, which is a transient state object:
var customer = new Customer () {Firstname = "Zhang", Lastname = "san"};
2. Persistent State (persistent)
Just been saved or just loaded from the database. An object is valid only for the associated ISession life cycle and has a corresponding record and identifier in the database. The object instance is managed by the NHibernate framework and, if there are any changes, synchronizes with the database when the action commits, and updates the object to the database.
3. Managed state (Detached)
After the isession of the persisted object is closed, the object is detached from the relationship in ISession, is the managed state, the managed object still has all the properties of the persisted object, and the reference to the managed object is still valid, and we can continue to modify it. If this object is re-associated to the ISession, it is again converted to persistent state, and changes in the managed period are persisted to the database.
Object state Transitions
In the case of synchronizing the database, execute the following statement to convert the state of the object.
Test the Validation object
Isession.contains (object): Checks if ISession contains the specified instance
Reset ISession
private void Resetsession ()
{
if (_session. IsOpen)
_session. Close ();
_session = _sessionmanager.getsession ();
_transaction. Session = _session;
}
1. Transient state transition Persistent state
Method One: Isession.save (): Saves the specified instance.
[Test]
public void Transientconvertpersistenttest ()
{
Instantaneous state object
var customer = new Customer () {Firstname = "Zhang", Lastname = "san"};
Assert.isfalse (_session. Contains (customer));
is still transient, CustomerID property value is empty
Association ISession saved to database
_session. Save (customer);
becomes persistent, because the CustomerID field in the table grows automatically, the CustomerID field automatically adds a
Returns the CustomerID property value after the NHibernate type conversion, ensuring that the database synchronizes with the instance object
Assert.istrue (_session. Contains (customer));
}
Method Two: Isession.saveorupdate (): Assign a new identity to hold the transient state object.
2. Persistent state transition to managed state
Method One: Isession.evict (object): Deletes the specified instance from the current ISession
[Test]
public void Persistentconvertdetachedevicttest ()
{
Customer customer = _transaction. Getcustomerbyid (1);
Assert.istrue (_session. Contains (customer));
_session. Evict (customer);
Assert.isfalse (_session. Contains (customer));
}
Method Two: Isession.close (): Close the current ISession
[Test]
public void Persistentconvertdetachedclosetest ()
{
Customer customer = _transaction. Getcustomerbyid (1);
Assert.istrue (_session. Contains (customer));
Resetsession ();
Assert.isfalse (_session. Contains (customer));
}
3. Managed state Transition Persistent state
Method One: Isession.update (): Updates the specified instance.
[Test]
public void Detachedconvertpersistentupdatetest ()
{
Customer customer = _transaction. Getcustomerbyid (1);
Persistent State Object
Assert.istrue (_session. Contains (customer));
Reset ISession
Resetsession ();
Assert.isfalse (_session. Contains (customer));
Managed state Object
Can continue to be modified under managed state
Customer. Firstname + = "Cnblogs";
_transaction. Updatecustomertransaction (customer);
Transition to persistent State object
Assert.istrue (_session. Contains (customer));
}
Take a look at this example: changes in the hosting period are persisted to the database;
Note: How does nhibernate know that the re-associated object is not "dirty (modified)"? If the new isession,isession cannot be compared with the object's initial value, the object is not "dirty", and we define the Unsaved-value attribute of the <id> element and the <version> element in the mapping file. NHibernate can judge for himself.
[Test]
public void Detachedconvertpersistentupdatealltest ()
{
Customer customer = _transaction. Getcustomerbyid (1);
Persistent State Object
Customer. Firstname + = "Zhang";
Assert.istrue (_session. Contains (customer));
Reset ISession
Resetsession ();
Assert.isfalse (_session. Contains (customer));
Managed state Object
Can continue to be modified under managed state
Customer. Firstname + = "Cnblogs";
Then update together
_transaction. Updatecustomertransaction (customer);
Transition to persistent State object
Assert.istrue (_session. Contains (customer));
}
Now we have the initial knowledge of the object's state. Although the details of the state of an object are maintained by NHibernate itself, the object state is more important in the NHibernate application. At the same time, the object state also involves NHibernate cache, offline query and so on.