Three state analysis of objects in hibernate

First, there are three states of objects in hibernate: transient state, free state and persistent state. The three state transition methods are all called through session, and the methods from transient to persistent state include save(), saveOrUpdate(), get( ), load(); methods from persistent state to transient state include delete(); methods from free state to persistent state include update(), saveOrUpdate(), lock(); methods from persistent state to free state include: session.close() , session.evict(), session.clear().

Second, the state of Hibernate

  Hibernate's various storage areas (save, persist, update, saveOrUpdte, merge, flush, lock) and three states of the object

  hibernate save

  Hibernate provides too many methods for object preservation, and there are many differences between them, so I will elaborate here to distinguish them.

  1. Preliminary knowledge

  Before all, let me explain that for hibernate, its objects have three states, transient, persistent, detached

  The following are common translation methods:

  transient: transient state or temporary state

  (new DeptPo(1, "Administration Department", 20, "Administration Related"), the instance of this po is not associated with the session, and the instance of this po is in transient)

  persistent: persistent state

  (And the Po instance that is recorded in the database to be mapped, its state is persistent, and the objects obtained through get and load are persistent)

  detached: detached state or free state

  (1) When the po objects obtained by the get or load method are persistent, but if delete(po) is executed (but the transaction cannot be executed), the po state is detached, (indicating that it is disconnected from the session), because delete The free state can be changed to a persistent state through save or saveOrUpdate()

  (2) When the session is closed, the persistent po object in the session cache also becomes detached

  If the session becomes free due to closing the session, it can become persistent through lock, save, and update.

  A persistent instance can be made detached by calling delete().

  Instances obtained through the get() or load() methods are persistent.

  Instances of detached state can be persisted by calling lock() or replicate().

  save() and persist() will trigger SQL INSERT, delete() will trigger SQLDELETE,

  And update() or merge() will cause SQL UPDATE. Modifications to persistent instances are detected during flush commits, which can also cause SQL UPDATEs.

  saveOrUpdate() or replicate() will cause SQLINSERT or UPDATE

  Second, the difference between save and update

  The reason for putting this pair in the first place is because this pair is the most commonly used.

  The function of save is to save a new object

  update is to update an object in a disjointed state or a free state object (must correspond to a record) to the database

  3. The difference between update and saveOrUpdate

  This is easy to understand. As the name suggests, saveOrUpdate basically synthesizes save and update, while update is just update; refer to a passage in the hibernate reference to explain their use cases and differences

  Usually the following scenarios will use update() or saveOrUpdate():

  The program loads the object in the first session, then closes the session

  The object is passed to the presentation layer

  The object has undergone some changes

  The object is returned to the business logic layer and eventually to the persistence layer

  The program creates a second session and calls the update() method of the second session to persist these changes

  saveOrUpdate(po) does the following:

  If the po object has been persisted in this session, executing saveOrUpdate in this session does nothing

  Throws an exception if savaOrUpdate(new po) has the same persistent identifier as another po object associated with this session

  org.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session: [org.itfuture.www.po.Xtyhb#5]

  saveOrUpdate if the object does not have a persistent identifier attribute, call save() on it, otherwise update() the object

  Fourth, the difference between persist and save

  This is the most confusing pair, on the surface it seems that either one can be used, and there is no clear distinction between them in the hibernate reference document.

  Here is a clear distinction. (You can follow up with src to take a look. Although the implementation steps are similar, there are still subtle differences)

  Main content differences:

  1. persist persists a transient instance, but does not "guarantee" that the identifier (the attribute corresponding to the identifier primary key) will be filled into the persistent instance immediately, and the filling of the identifier may be delayed until flush.

  2, save, persist a transient instance identifier, generate it in time, it will return the identifier, so it will execute Sql insert immediately

  Five, saveOrUpdate, merge and update difference

  Compare update and merge

  The function of update is mentioned above, here is the merge

  If there is an instance of the same persistent identifier (identifier) ​​in the session, overwrite the existing persistent instance of the session with the object given by the user

  (1) When we use update, after the execution is completed, an exception will be thrown

  (2) But when we use merge, copy the attributes of the po object A dealing with the free state to the attributes of the po in the persistent state in the session. After the execution is completed, it turns out to be a persistent state or a persistent state, and the A we provide still free

  6. The difference between flush and update

  The difference between the two is easy to understand

  The update operation is an object in a free state or a disconnected state (in a disconnected state due to the closure of the session) //updateSQL

  And flush is an object that operates on persistent state.

  By default, changes to a persistent state object (including set containers) do not require update. As long as you change the value of the object, it will be automatically updated or saved to the database after waiting for hibernate flush. hibernate flush occurs in the following situations:

  1. The combination of calling some queries and manual flush(), closing the session and closing the SessionFactory

  get() an object, change the properties of the object, and close the resource.

  2. When transaction commits (including flush)

  Seven, the difference between lock and update

  update is to turn an object that has changed in a decoupled state into a persistent state

  lock is to turn an object that has not changed into a detached state into a persistent state (for the po object (2) that is in the detached state due to the closure of the session, not for the po object that is in the detached state due to delete)

  Corresponding to changing the content of a record, the two operations are different:

  The operation steps of update are:

  (1) Modification of detached objects after attribute changes -> call update

  The operation steps of lock are:

  (2) Call lock to change the unmodified object from the unmanaged state to the persistent state --> change the content of the object in the persistent state --> wait for flush or manual flush

  Eight, the difference between clear and evcit

  clear completely clears the session cache

  evcit(obj) clears a persistent object from the session's cache.

  session.lock(xtyhb,LockMode.NONE);//Indicates that you go directly to the cache to find objects that become persistent

  session.lock(xtyhb,LockMode.READ);//First read the ID of the record in the database through the ID to see if there is a record, if there is, then go to the cache to find the object that becomes persistent




 



 

In Hibernate, objects have three states: temporary state, persistent state and free state.

Also called: Transient, Persistent, Detached. Objects in persistent state are also called PO (Persistence Object), and transient objects and detached objects are also called VO (Value Object).

Temporary state: When a new entity object is created, the object is in a temporary state, that is, the object is only a memory area that stores temporary data. If no variable references this object, it will be recycled by the jre garbage collection mechanism. The data saved by this object has nothing to do with the database, unless the temporary object is associated with the database through save or SaveOrUpdate of the Session, and the data is inserted or updated to the database, the object is converted into a persistent object.
For example: Emp e=new Emp(); //Create a temporary object
          e.setEmpno((long) 8888);
          e.setEName("mike");
          ...
          EmpDAO d=new EmpDAO();
          d.save(e ); //Persist
          ...

Persistent state: Instances of persistent objects have corresponding records in the database and have a persistent representation (ID). After the delete operation is performed on the persistent object, the corresponding record in the database will be deleted, so the corresponding relationship between the persistent object and the database record no longer exists, and the persistent object becomes a temporary state.
    After the persistent object is modified, it will not be synchronized to the database immediately until the database transaction is committed. Before synchronization, persistent objects are dirty (Dirty).
For example:
          Emp e=new Emp(); //Create a temporary object
          EmpDAO edao= new empDAO();
          e=edao.findbyEmpno((long) 7786); //Make the object correspond to the database record, so as to persist
          e .setEname("new name"); //Modifies the persistent object to be in Dirty
          ......
          edao.saveorupdate(e); //Save, but still Dirty
          tran.commit(); // Commit, achieve synchronization with database, no longer Dirty
          ...

Free state: When the session is Close, Clear or evict, the persistent object has a persistent identifier and a value consistent with the corresponding record in the database, but because the session has disappeared, the object is not in the persistent management, so it is in a free state (Also called: out of tube state). A free state object is very similar to a transient state object, except that it also contains a persistent identifier.

 


The transient state
        is a java object whose memory space is opened up by the new command,
       eg. Person person = new Person("xxx", "xx");
        If there is no variable to reference the object, it will be reclaimed by the java virtual machine.
         Transient objects exist in isolation in memory. They are carriers of information and do not have any relationship with database data. In Hibernate, transient objects can be associated with the database through the save() or saveOrUpdate() methods of the session, and the The corresponding data is inserted into the database, and the transient object is transformed into a persistent object at this time.
The object in the persistent state
        has a corresponding record in the database and has a persistent identifier. If the delete() method of hibernate is used, the corresponding persistent object becomes a transient object. Since the corresponding data in the database has been deleted, the object is no longer associated with the records of the database.
       When a session executes close(), clear(), and evict(), the persistent object becomes a detached object. At this time, the persistent object will become a detached object. At this time, although the object has a database identification value, it is no longer in control. Under the management of the HIbernate persistence layer.
       Persistent objects have the following characteristics:
        1. Associated with the session instance;
       2. There are records associated with it in the database.
Detached state
        When the session associated with a persistent object is closed, the persistent object becomes a detached object. When the detached object is re-associated with the session, it becomes a persistent object again.
       The detached object has the identification value of the database, and can be transformed into a persistent object by methods such as update() and saveOrUpdate().
       The detached object has the following characteristics:
       1. It is essentially the same as a transient object. When no variable refers to it, the JVM will recycle it at an appropriate time;
        2. It has an additional database record identification value than the transient object.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326401767&siteId=291194637