[Nhib.pdf] object status
The previous two articles introduced the use of SchemaExport. This tool can be used to generate a database architecture based on the ing file. This article will introduce three object states in nhib.pdf. When the program is running, a series of persistent objects are generated while operating the database using objects. These objects may be created and ready to be stored, or obtained from the database. to distinguish these objects, based on the association status between the objects and the current Session, objects can be divided into three types: instantaneous objects: objects that have just been created are not stored in the database or cached in the ISession. If the primary key of the object is automatically created, the object identifier is blank. Persistent Object: the object has been persisted through nhib.pdf, and the record of this object already exists in the database. If this object automatically creates a primary key, the object identifier has been assigned a value. Managed object: this object has been saved or retrieved from the database through nhib.pdf, but the ISession associated with this object has been disabled. Although it has an object identifier and has corresponding records in the database, it is not managed by nhib.pdf. Object state (nhib.pdf) provides object state management. It supports three object states: Transient, Persistent, and Detached ). The Transient object has just been created and has not been associated with the ISession. At this time, the instantaneous object will not be persisted into the database, nor will it be assigned an identifier. If not, it is destroyed by GC. The ISession interface can convert it to a persistent state. For example, you just created a Customer object, which is an instantaneous object. Copy code 1 // Instantaneous State object 2 var customer = new Customer () 3 {4 CustomerID = Guid. newGuid (), 5 NameAddress = new Name () 6 {7 CustomerAddress = "Beijing", 8 CustomerName = "wolfy" 9}, 10 Orders = null, 11 Version = 1 12}; copy the Persistent code that was just saved or loaded from the database. The object is valid only within the lifecycle of the associated ISession, and has corresponding records and identifiers in the database. The object instance is managed by the nhib.pdf framework. If any changes occur, the instance is synchronized with the database at the time of submission, and the object is saved and updated to the database. After the ISession associated with the Detached Persistent Object is disabled, the object is out of the relationship in the ISession, that is, the managed State. The managed object still has all attributes of the Persistent Object, the reference to the managed object is still valid. We can continue to modify it. If this object is re-associated with ISession, it is converted to a persistent state again. Modifications made during the hosting period will be persisted to the database. The following statement can be executed to convert the object state when the object state is synchronized to the database. ISession. contains (object): Check whether the ISession Contains the reset method for adding an ISession to the specified instance. Copy code 1 public static ISession ResetSession () 2 {3 if (_ session. isOpen) 4_session. close (); 5 _ session = _ sessionFactory. openSession (); 6 return _ session; 7} Method 1: ISession. save (): Save the specified instance. Copy code 1 public void TransientConvertPersistent () 2 {3 // Instantaneous State object 4 var customer = new Customer () 5 {6 CustomerID = Guid. newGuid (), 7 NameAddress = new Name () 8 {9 CustomerAddress = "Beijing", 10 CustomerName = "wolfy" 11}, 12 Orders = null, 13 Version = 1 14}; 15 ISession session = NHibernateHelper. getSession (); 16 if (! Session. contains (customer) 17 {18 // The associated ISession is saved to 19 sessions in the database. save (customer); 20} 21 // Changes to the persistent State, because the CustomerId field in the table automatically grows (if it is an Automatically increasing primary key), Save the database, the CustomerId Field automatically increases by 22 // The CustomerId attribute value is returned after the NHibernate type conversion to ensure that the database and instance object are synchronized 23 if (session. contains (customer) 24 {25 26} 27} copy code Method 2: ISession. saveOrUpdate (): assign a new identifier to save the Instantaneous State object. Method 1 of converting persistent state to hosted state: ISession. evict (object): Delete the specified instance from the current ISession and copy code 1 public void PersistentConvertDetachedEvict () 2 {3 // get session 4 ISession session = NHibernateHelper. getSession (); 5 // obtain the customer object 6 var customer = session based on the id. get ("Customer", new Guid ("DDF63750-3307-461B-B96A-7FF356540CB8"); 7 // Delete 8 if (session. contains (customer) 9 {10 session. evict (customer); 11} 12} copy code Method 2: ISessio N. close (): Close the current ISession copy code 1 public void PersistentConvertDetachedCloseTest () 2 {3 // get session 4 ISession session = NHibernateHelper. getSession (); 5 // obtain the customer object 6 var customer = session based on the id. get ("Customer", new Guid ("DDF63750-3307-461B-B96A-7FF356540CB8"); 7 if (session. contains (customer) 8 {9 NHibernateHelper. resetSession (); 10} 11} Method 1: ISession. update (): Update a specified instance. Copy code 1 public void DetachedConvertPersistentUpdateTest () 2 {3 // get session 4 ISession session = NHibernateHelper. getSession (); 5 // obtain the customer object 6 Customer customer = session based on the id. get ("Customer", new Guid ("DDF63750-3307-461B-B96A-7FF356540CB8") as Customer; 7 // reset ISession 8 NHibernateHelper. resetSession (); 9 // managed State object 10 // you can continue to be modified 11 if (session. contains (customer) 12 {13 customer. nameAddress = ne W Name () {CustomerAddress = "Shanghai", CustomerName = "wolfy"}; 14 // convert to persistent State object 15 session. update (customer); 16} 17} copy the code. The above example shows that the modifications made during the hosting period will be persisted into the database. Note: how does nhib.pdf know if the re-associated object is "Dirty (modified )"? If it is a new ISession, ISession cannot compare it with the initial value of the object to see if the object is "dirty ", we define the unsaved-value Attribute of the <id> element and <version> element in the ing file, and NHibernate can determine it by ourselves. If a lock is added: If the update statement is not modified during the hosting period, the update statement is not executed and only converted to the persistent State. In the following example, if the object is modified during the hosting period, the update statement is executed. Copy code 1 public void DetachedConvertPersistentUpdateLockTest () 2 {3 // get session 4 ISession session = NHibernateHelper. getSession (); 5 // obtain the customer object 6 Customer customer = session based on the id. get ("Customer", new Guid ("DDF63750-3307-461B-B96A-7FF356540CB8") as Customer; 7 if (session. contains (customer) 8 {9 NHibernateHelper. resetSession (); 10} 11 // lock 12 sessions. lock (customer, nhib.pdf. lockMode. none); 13 // If no modification is made during the hosting period, the update statement is not executed and only converted to the persistent State 14 session. update (customer); 15} When copying code, pay attention to the use of the Lock. Ensure that the customer object is not null. Therefore, no judgment is made for the convenience of testing. If it is null, there will be an exception (attempt to lock null ). Method 2: ISession. Merge (): Merge the specified instance. You do not need to consider the ISession status. If the ISession contains persistence objects of the same identity, nhibsion overwrites the original persistence instance status based on the object status given by the user. Method 3: ISession. SaveOrUpdate (): assign a new identifier to save the Instantaneous State object, and update/reassociate the managed State object.