Getting Started 16 - Object Status and Identification
After the previous topic, after the basic operation of Hibernate and ORM, let's revoke some of the mechanisms of the Hibernate underlying, first discussing from Hibernate's object status. Objects in Hibernate can be divided into three states: Transient objects, persistent objects, and detached objects. The temporary object refers to the object of the Java program process, for example, in the previous example, the object derived by the User category, and is a temporary object before (), these Objects have no relationships with persistence layers, as long as there is no name reference to this object, the object will be reclaimed when appropriate. If the temporary object stores its status to a persistent layer using the Save () or SaveorUpdate () method, the temporary object transitions to a persistent object, and the lasting object will have the same identification as the database, in our object In the design, it is the value of the object's ID attribute (given), which is the same as database identity. If you are using the data object returned directly, for example, the data objects are queried using Find (), get (), load (), which are related to the fields in the database, with the same identification value. ID value, they also become a persistent object. In addition, if a temporary object is taken for a long-lasting object, the object will automatically become a lasting object, which is a persistent manager (PERSISTANCE MANAGER) to check the associated and acting through the calculation, this later theme will also Discussion. Persistent object If you use Delete (), you will change back to the teleconal object, this is of course, if you use Delete (), there will be no corresponding to the object in the database, the object is no longer any Relationship. The lasting object is always associated with Session and Transaction, in a session, the change of persistent objects will not change the database immediately, but must be terminated in the transaction, which is executed, and after the database is actually running SQL in the database. Change, the state of lasting objects will synchronize with the database, and we call Dirty before synchronization. When a session executes Close () or clear (), EVICT (), the persistent object becomes a separation object, although the ID of the object is identified, but they are not currently managed by Hibernate persistence Below, it is essentially the same as the temporary object, and when there is no name to them, it will be recovered when it is appropriate. The separation object has a database identification value, so it can be connected to the persistent layer by update (), saveorupdate (), lock (), etc., the usual application is, you query a data from the database, close the session to connect The resources are made, and the object is the separation state, and the user operates the separation object (like a shopping cart) after a short period of time, then re-opening a session, associates the separation object with the session, and then store the change back to the database.
After approximate the object state, we also discuss the object identification problem. For the database, it recognizes a uniqueness of the data is based on the primary key value, if there are two data on the hand, they have the same primary key value. Then they represent the data of the same field in the database. For Java, there are two ways to identify whether two objects are the same object, one is determined according to whether or not the object has the same memory location, in the Java syntax, it is compared to == operation, one is Definitions in equals (), Hascode (). First, the first Java recognition method is discussed in the place in Hibernate. In Hibernate, if it is the same data obtained in the same session according to the same query, they will have the same Java recognition, and actually Example to explain: session session = sessions.opensession ();
Transaction tx = session.begintransaction ();
Object obj1 = session.load (user.class, new long (007));
Object Obj2 = session.load (user.class, new long (007));
TX.comMit ();
session.close ();
System.out.println (Obj1 == Obj2);
The above program clip will display the result of TRUE, indicating that OBJ1 and OBJ2 are reference to the same object, but if it is the case, FALSE is not displayed:
SESSION session1 = sessions.opensession ();
Transaction tx1 = session1.begintransaction ();
Object obj1 = session1.load (user.class, new long (007));
TX1.COMMIT ();
Session1.close ();
SESSION session2 = sessions.opensions ();
Transaction tx2 = session1.begintransaction ();
Object obj1 = session2.Load (user.class, new long (007));
Tx2.commit ();
session2.close ();
System.out.println (Obj1 == Obj2);
Simply put, in two different sessions, Hibernate does not guarantee that both objects have the same reference. If you want to compare the data obtained by two different session, you can define Equals () and Hascode (), define your actual demand, and compare objects using Obj1.equals (OBJ2) Really worthless, let's discuss the relevant details in the next topic.