SSH framework-transition of three statuses of hibernate, ssh-hibernate

Source: Internet
Author: User

SSH framework-transition of three statuses of hibernate, ssh-hibernate
1. amazing things

When I used the findAll () method to investigate a List object, I performed some operations on the list object without calling the update or saveOrUpdate method, the changed data is magically saved to the database.

Finally, the simple and crude solution is to copy the List found from the data, operate it, and return the result. The data is normal, and the database is not updated. Later, I found out that jpa is the encapsulation of hibernate, and hibernate is the underlying layer. This is the ghost of hibernate's persistent state.

Ii. Three statuses of hibernate

1. Transient status (Transient)

When we use the new Keyword of Java to generate an object, the object is in the Free State, as shown below:

1 Customer customer = new Customer ("zx", 27, images); at this time, the customer object is in the Free State. Why do we say that the customer object is in the Free State? This is because, at this time, the customer only obtains a piece of memory space through JVM and has not been saved to the database through the save () method of the Session object. Therefore, it is not included in the Cache Management of Hibernate, that is to say, the customer object is still freely wandering in addition to Hibernate Cache Management. So we can see that the biggest feature of a free object is that there is no record corresponding to it in the database.

Instantaneous Object Features: there is no record associated with the instantaneous object in the database that is not associated with the Session instance

2. Persistent)

A persistent object is an object that has been saved to the database and is still in the Cache Management of Hibernate. This is any modification to the object and will be synchronized to the database when the cache is cleared. As follows:

Customer customer = new Customer ("zx", 27, images );

Tx = session. beginTransaction ();

Session. save (customer );

Customer = (Customer) session. load (Customer. class, "1 ");

Customer. setAge (28 );

Tx. commit ();

At this time, we did not display the call session. the update () method is used to save updates, but modifications to object objects are synchronized to the database, because after the customer object is saved to the database through the save method, it is already a persistent object, then load it again through the load method, it is still a persistent object, so it is still in the Hibernate Cache Management, then when the execution of tx. when the commit () method is used, Hibernate automatically clears the cache and automatically synchronizes the attribute changes of the Persistent Object to the database.

Persistent instances have corresponding records in the database and have a persistent identifier (identifier ).

Persistent objects are always associated with sessions and transactions. In a Session, changes to persistent objects do not immediately change the database, but must be terminated in Transaction, that is, the execution of commit () then, the SQL statement is run in the database to make changes, and the status of the Persistent Object is synchronized with the database. A persistent object before synchronization is called a dirty (dirty) object.

Convert an instantaneous object to a persistent object:

Associate an instantaneous object with the database through the save () and saveOrUpdate () Methods of the Session, and this instantaneous object becomes a persistent object. Data objects to be queried using fine (), get (), load (), and iterater () methods will become persistent objects. Persistence object features:

Records associated with Session instances and persistent objects in the database 3. Detached)

When a persistent object is removed from the Cache Management of Hibernate, it is in the Free State. The biggest difference between a free object and a free object is that, the free object may still have a record corresponding to it in the database, but now the free object is out of the Cache Management of Hibernate, free objects do not have data records corresponding to them in the database. As follows:

Customer customer = new Customer ("zx", 27, images );

Tx = session. beginTransaction ();

Session. save (customer );

Customer = (Customer) session. load (Customer. class, "1 ");

Customer. setAge (28 );

Tx. commit ();

Session. close ();

When the session is closed, the customer object is not in the Cache Management of Hibernate, but there is still a data record corresponding to the customer object in the database, therefore, when the Session associated with the Free State of the customer object with the persistent object is closed, the object becomes an unmanaged object. The reference to the unmanaged object is still valid, and the object can be modified.

Features:

Essentially, it is the same as the instantaneous object. It is only one more database record id value than the instantaneous object of love. The Persistent Object is converted to the offline object:

After close (), clear (), and evict () are executed, the persistent object is changed to an unmanaged object.

Convert an instantaneous object to a persistent object:

You can use the update (), saveOrUpdate (), lock (), and other methods of the Session to change the unmanaged object to a persistent object.

Iii. Transition of three states

 

 

Iv. Example

Combine the save (), update (), and saveOrUpdate () Methods to describe the object status

(1) The Save () method saves the instantaneous object to the database, and the temporary state of the object changes to the persistent state. When the object is in the persistent state, it is always in the Session cache and any operations on it will be synchronized to the database when the transaction is committed. Therefore, it is meaningless to call the save () or update () method for a persistent object. For example:

Student stu = new Strudnet ();

Stu. setCarId ("200234567 ");

Stu. setId ("100 ");

// Open the Session and start the transaction

Session. save (stu );

Stu. setCardId ("20076548 ");

Session. save (stu); // invalid

Session. update (stu); // invalid

// Submit the transaction and close the Session (2) update () method for two purposes: Re-associate the unmanaged object as a persistent State object. update () is called to update the object. Update () is called only to associate an offline object to the persistent state. When the object is already in the persistent state, it makes little sense to call update. For example:

// Open the session and start the transaction

Stu = (Student) session. get (Student. class, "123456 ");

Stu. setName ("Body ");

Session. update (stu); // because stu is a persistent object, it must be in the Session buffer,

Changes made to stu will be // synchronized to the database. Therefore, update () does not make sense. You can avoid the same effect.

// Submit the transaction and close the Session

Hibernate always executes the update statement. No matter whether the unmanaged object has been changed after it leaves the Session, Hibernate always sends an update statement when clearing the cache, to ensure that the data of the unmanaged object is consistent with that of the database record, for example:

Student stu = new Strudnet ();

Stu. setCarId ("1234 ");

// Enable Session1 and transaction

Session1.save (stu );

// Submit the transaction and close Session1

Stu. set ("4567"); // modify the object that is not managed

// Enable Session2 and transaction

Session2.update (stu); ssh

// Submit the transaction and close Session2

Note: Even if session2.update (stu) is removed, an update () statement is still executed when the transaction is committed.

If you want to generate an update statement only when the de-managed object changes, you can set select-before-update to true for the tag in the ing file, in this case, a select statement is sent to obtain the value in the database and determine whether the value is the same. If the value is the same, the update statement is not executed. However, this method has some disadvantages. Before each update statement, a redundant select statement is always sent, which affects the performance. For occasionally changed classes, the setting is effective, and for frequently changed classes, this affects the efficiency.

(3) The saveOrUpdate () method provides both the save () and update () methods. For an input object, saveOrUpdate () first checks whether the object is an unmanaged object or a temporary object, then call the appropriate method.

Reference

State and transformation of Hibernate object by javacoffe

We recommend a communication and learning group: 650385180, which will share some video recordings recorded by senior Architects: Spring, MyBatis, Netty source code analysis, high concurrency, high performance, distributed, and microservice architecture principles, JVM performance optimization is a required knowledge system for architects. You can also get free learning resources, which has benefited a lot:

 

Note: If you like it, you can likes and pay attention to it and learn and make progress together.

 

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.