Learn more about the three states of Hibernate persistent objects

Objects in Hibernate have three states: transient objects, persistent objects, and offline objects.

Figure 3.1 below shows the relationships between transient objects, persistent objects, and offline objects, and the transformations between them.

Figure 3.1

Temporary state: a Java object whose memory space is opened up by Java's new command, that is, an ordinary Java object. If there is no variable reference to it, it will be reclaimed by the JVM. Temporary objects exist in isolation in memory. Its meaning is to carry information carrier and not be associated with the data in the database. Through the save () method and saveOrUpdate () method of session, you can associate a temporary object with the database, and insert the information carried by the temporary object into the database through the mapping made by the configuration file. This temporary object becomes a persistent object.

Persistent state: persistent objects have corresponding records in the database. Persistent objects can be just saved or just loaded, but they are saved in the associated session declaration cycle. If it is the data objects returned by the direct database query, these objects are associated with the fields in the database and have the same ID. they immediately become persistent objects. If a temporary object is referenced by a persistent object, it becomes a persistent object immediately. If you use the delete () method, the persistent object becomes a temporary object, and the corresponding record in the database is deleted, and the object is no longer associated with the database.

Persistent objects are always associated with sessions and transactions. In a session, operations on persistent objects will not be written to the database immediately. Only when the transaction ends can the database be really updated, so as to complete the synchronization of persistent objects and databases. Persistent objects before synchronization become dirty objects.

When a session () executes close (), clear (), or evict After (), the persistent object becomes an offline object. At this time, although the ID of the object has the identification value of the database, it is no longer under the management of the hibernate persistence layer. It is basically the same as the temporary object, but it has more database identification ID than the temporary object. When there is no variable reference, the JVM reclaims it.

Unmanaged status: after a session is closed, the persistent object associated with this session becomes an unmanaged object. You can continue to modify this object. If the unmanaged object is re associated with a new session, it will become a persistent object here.

Although the unmanaged object has the user ID, it is associated with the persistence layer again through methods such as update(), saveorupdate().

Next, we use hibernate to add, delete, change and query the database to reflect the transformation process between the three states.

Adding and modifying demonstrates the changes between the three states

When we create a session, we need to instantiate the sessionfactory, so we encapsulate the repeated code, and the session is single threaded. We encapsulate the management of session, opening session and closing session into the tool class. The code is as follows.

Hibernate. cfg. The XML code is shown below.

Previously, we put the operations added to the table into ordinary Java classes, It is executed in the main () method of this class. What if we do other operations on the table? Is it necessary to create a new Java class, and multiple methods are not easy to test? We use the test tool class JUnit to test, add, delete, modify and check. First, establish the source directory and put the test program in the test package.

We build our test program sessiontest Java, inherit the testcase class, so we can use it in sessiontest Java class tests a method in the database, and the specification of method name should start with test. We add a record to the user table, as shown in the following code.

First, establish a session between the object and the table and start the transaction session Begintransaction(), instantiate the user object, When user = new user(), when we create a new object, the database does not exist, so it is a transient object (temporary object), and then assign a value to the user, set the name, password and other attributes. After assigning values to all the attributes of the object, save them to the session, get the session and execute save (user) method. When we execute the save () method of the session, this object is managed by the session. There is a map in the session, and the object will be placed in the map. At this time, the object will become a persistent state (persistent state).

Next, we set the name attribute in the user to "Li Si", and then commit the transaction. We first store "Zhang San" in the session, and then change it to "Li Si". Try catch to catch exceptions. When the execution is completed and the session is closed, the object is in the detached state (offline state).

We create the database and use exportdb Java method to create a table. Then execute the testsave1() method of sessiontest. When the session method is executed, the ID value of the user table is automatically generated in the table, and the name is "Zhang San". Then execute it again, and the name becomes "Li Si". Then execute the commit() method TX. Commit of the transaction. At this time, the console will issue a statement, as shown in Figure 3.2.

It can be seen from the console statement that the inserted SQL statement sent is displayed, followed by the update statement. First, the name of the persistent object user is "Zhang San", so the inset statement is generated when saving. At this time, the user is in a persistent state, and we later changed it into a persistent object, so we sent a modification statement. That is, when the persistent object is modified, we commit the transaction, All the changes will be reflected (UPDATE statement). That is, when we commit the transaction again, we clean the cache, that is, check the dirty data (the memory has changed, but the data has not changed), check which data is problematic, and keep the memory in step with the database. Therefore, the name of the user added to the database is Li Si (as shown in Figure 3.3).

Figure 3.3

If in the above code, we change the name to Li Si and then user Setname ("Li Si"); We show that when we call the update () method of session and run session. Update (), we will see the SQL statements printed on the console and those we do not add, such as session Update() prints the same. As long as the persistent object is changed, it will be synchronized when the transaction is committed. There is no need to display the call.

Detached status demo

After all operations are performed and the session is closed, the user object changes to the detached state, and the operation is performed at this time.

The code is shown below.

Get the user object in detached state, change the name value of this object, user Setname ("Wang Wu"); Then we'll create a new one

Session, start the transaction through session, and then update the operation, session Update (user), that is, bring the offline object (or unmanaged object) into session management, which will be synchronized with the database. Because session. Update () brings the user object into session management, and the user object changes from offline state to persistent state.

Commit the transaction and synchronize with the database. Reflect the memory changes to the database. The console SQL statement and the results of adding records to the table are shown in figures 3.4 and 3.5.

Figure 3.4

summary

The above is the three states of Hibernate persistent objects introduced by Xiaobian to you. I hope it will help you. If you have any questions, please leave me a message, and Xiaobian will reply to you in time. Thank you very much for your support for the programming tips website!

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>