In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly introduces the "transition mode between Hibernate object states". In daily operation, I believe that many people have doubts about the transition mode between Hibernate object states. The editor has consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful for you to answer the doubts about "the transition mode between Hibernate object states". Next, please follow the editor to study!
State classification
In the Hibernate framework, to manage persistence classes, Hibernate divides them into three states:
Transient state (Transient Object) persistent state (Persistent Object) out-of-tube state (Detached Object)
There are many people do not seem to understand these concepts and the transformation between them, so this article is to solve these problems, you will not come to me after reading. (just kidding)
Detailed description
Let's first take a closer look at the three states:
1. Instantaneous state
Objects that are created by the new operator and that are not already associated with the Session in Hibernate are considered to be in a transient state. The transient object will not be persisted to the database, nor will it be given a persistence identity. If the reference to the transient object is lost in the program, the transient object will be destroyed by the garbage collection mechanism.
2. Persistent state
The persistence instance has a corresponding record in the database and has a persistence identity. Instances of persistence can be just saved or just loaded. Either way, the persisted object must be associated with the specified Session object.
3. Out-of-tube state
An instance was once in a persistent state, but when the Session associated with it was closed, the object became out of control. References to the out-of-control state are still valid and the object can continue to be modified. If you re-associate an out-of-pipe object with a Session, the out-of-pipe object transitions back to a persistent state.
Whether the transient state persistent state unmanaged state is stored in the Session cache × √ × database has a corresponding record × √ √
For example:
Public class HibernateTest {private Session session;private Transaction transaction;@Beforepublic void before () {session = HibernateUtil.getSession (); transaction = session.beginTransaction ();} @ Afterpublic void after () {transaction.commit (); session.close ();} @ Testpublic void test () {Person p = new Person (); p.setPname (Zhang San); p.setAge (20); session.save (p);}}
So in such an example, from creating a Person object to assigning values to the name and age properties, these processes are in a transient state, and when the save () method of the session object is called, the object changes from transient to persistent. When session is turned off, the object changes from persistent state to unmanaged state.
Transition between object states
Now that we understand the concepts related to the three object states, let's take a look at how the three object states are miraculously transformed to each other.
Transient persistent state
We know that when an object is created, it is in a transient state, so how does it convert to a persistent state? Look at an example:
@ Testpublic void test () {Person p = new Person (); p.setPid (2); p.setPname ("Li Si"); p.setAge (30); session.save (p); / / session.saveOrUpdate (p);}
As mentioned earlier, the object is still in a transient state when assigning values to the name and age properties. It is important to note, however, that when you assign a value to the primary key, the Pid property, the object will no longer be in a transient state, but will be converted to an unmanaged state, because there is already a persistence identity, but there is no association with Session. The session object is converted to a persistent state only when the update () or saveOrUpdate () method of the object is called. Of course, the objects queried from the database by calling the get (), load (), query, find (), and other methods of the session object are also in a persistent state. It is only when the session object calls the delete () method to delete a persistent object from the database that the object changes from persistent state to transient state.
Persistent out-of-tube state
When the close (), clear () and other methods of the session object are called, the objects associated with the session will change from the persistent state to the unmanaged state, and these objects will lose the association of the relevant session. To switch from the unmanaged state to the persistent state, simply call methods such as save (), saveOrUpdate (), and so on.
Transient state-- > out-of-tube state
As mentioned earlier, when the setXXX () method is called to set the primary key property after the object is created, the object changes from transient to unmanaged state, provided that the primary key exists in the database.
Object life cycle
The following is an analysis of an object from creation to saving to the database:
Try {Session session = HibernateUtil.openSession (); / / start transaction session.beginTransaction (); / / person object enters transient state Person p = new Person (); p.setPname (Wang Wu); p.setAge (40); / / person object enters persistent state session.save (p); / / commit transaction, implicitly including the action session.getTransaction (). Commit () of session.flush () / / after the submission is completed, the person is free} catch (HibernateException e) {e.printStackTrace (); if (session! = null) session.getTransaction (). Rollback ();} finally {if (session! = null) session.close ();}
When an object is instantiated, the object is in an instantaneous state. When session.save (Object) is called, the object is added to the session cache and goes into a persistent state. At this time, there is no corresponding record in the database. When session commits the transaction, the database generates the corresponding record, but it should be noted here, because when the transaction commits, the session.flush () method is called by default to clear the cache, which is equivalent to calling the clear () method, and we know that when the clear () method is called, the object will change from persistent state to unmanaged state. Objects in the unmanaged state will be destroyed by the garbage collection mechanism. This is the complete life cycle of an object from creation to preservation to the database.
Other
After having a certain understanding of the state of the object, it can be used to explain many phenomena.
In Hibernate, only when the object changes from other state to persistent state, it will automatically generate sql statements, other times will not repeatedly generate sql, which is the key to improve the efficiency of the Hibernate framework.
For example:
@ Testpublic void test2 () {Session session = HibernateUtil.getSession (); Transaction transaction = session.beginTransaction (); Person p = new Person (); p.setPname (Li Si); p.setAge (30); session.save (p); p.setPname (Wang Wu); session.update (p); transaction.commit (); session.close ();}
I turn off a power on the statement transaction.commit ();, and then debug it.
As you can see, the console outputs only one sql statement, the insert statement generated when the save () method is executed, but the execution of the update () method does not generate sql. This is because when the update () method is executed, the Hibernate framework determines the state of the current object. It finds that the current object is in a persistent state, so it does not generate sql repeatedly, but changes the value of the persistent object, and then calls the commit () method to generate an update statement when the transaction commits.
Let's move on to an example:
@ Testpublic void test2 () {Session session = HibernateUtil.getSession (); Transaction transaction = session.beginTransaction (); Person p = new Person (); p.setPname ("Zhang San"); p.setAge (30); session.save (p); / / when the object changes from transient to persistent, generates sql statements p.setPname ("Wang Wu"); session.save (p); / / the object is persistent and does not generate sql statements p.setPname ("Zhao Liu"); session.update (p) / / at this time, the object is persistent and no sql statement transaction.commit (); session.close ();} is generated.
You know, it doesn't matter which method you call, it depends on the state of the object, and sql statements are generated only when you change to a persistent state. So the above segment still produces only two sql, one generated by the save () method and the other by the commit () method. The console information is as follows:
Hibernate: insert into PERSON (PNAME, AGE) values (?) Hibernate: update PERSON set PNAME=?, AGE=? Where PID=?
Understanding the three states of Hibernate will make it easier to understand how Hibernate works, which will give you critical help in locating doubtful issues in your development.
At this point, the study of "the transition between the states of Hibernate objects" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.