In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)05/31 Report--
This article introduces to you what are the three states of objects in hibernate, the content is very detailed, interested friends can refer to, hope to be helpful to you.
First, there are three kinds of states of objects in hibernate: transient, free state and persistent state. The three methods of state transformation are all called through session. The methods of transient to persistent state are save (), saveOrUpdate (), get () and load (); the methods of transient state to transient state are delete (); the methods of free state to persistent state are update (), saveOrUpdate (), lock () The methods from persistent state to free state are: session.close (), session.evict (), session.clear ().
Second, the status of Hibernate
The save,persist,update,saveOrUpdte,merge,flush,lock of various preservation methods of hibernate and the three states of objects
Preservation of hibernate
Hibernate provides too many ways to save objects, and there are many differences between them, which can be distinguished in more detail here.
I. preliminary knowledge
Before all this, let's say that for hibernate, its objects have three states, transient, persistent, and detached.
The following are common translation methods:
Transient: transient state or temporary state
(new DeptPo (1, "Administration Department", 20, "Administration related"), the instance of the po is not associated with the session, and the instance of the po is in transient)
Persistent: persistence statu
(and record the Po instance you want to insinuate in the database, its status is persistent, and the objects obtained through get, load, etc., are all persistent)
Detached: out-of-tube state or free state
(1) when the po objects obtained by the get or load method are in persistent, but if the delete (po) is executed (but the transaction cannot be executed), the po state is in detached, (which means it is disconnected from session), and the free state due to delete can be changed into persistent state through save or saveOrUpdate ().
(2) when session is turned off, the po object of persistent in session cache also becomes detached.
Those who become free due to turning off session can become persistent through lock, save and update.
Persistent instances can become out-of-pipe by calling delete ().
The instances obtained through the get () or load () methods are persistent.
Instances of unmanaged state can be persisted by calling lock () or replicate ().
Save () and persist () will raise SQL's INSERT,delete () will throw SQLDELETE
Update () or merge () throws a SQL UPDATE. Changes to persistent (persistent) instances are detected when the commit is refreshed, and it also causes SQL UPDATE.
SaveOrUpdate () or replicate () throws SQLINSERT or UPDATE
II. 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 out-of-control object or a free-state object (which must correspond to a record) to the database
III. The difference between update and saveOrUpdate
This is easy to understand. As the name implies, saveOrUpdate is basically a synthesis of save and update, while update is just update; quoting a passage from hibernate reference to explain where they are used and the difference.
Update () or saveOrUpdate () is usually used in the following scenarios:
The program loads the object in the first session and then closes the session
The object is passed to the presentation layer
Some changes have taken place in the object
The object is returned to the business logic layer and finally 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
If savaOrUpdate (new po) has the same persistence identity (identifier) as another po object associated with this session, an exception is thrown
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 persistence identity (identifier) attribute, call save () on it, otherwise update () this object
IV. The difference between persist and save
This is the most confusing couple. On the surface, you can use either one, and there is no clear distinction between them in the hibernate reference document.
A clear distinction is made here. (you can follow up with src to see that although the implementation steps are similar, there are still slight differences.)
Main content differences:
1Perfect persists a transient instance, but there is no guarantee that the identifier (the attribute corresponding to the identifier primary key) will be populated into the persistent instance immediately, and the filling of the identifier may be delayed until flush.
2 Sql insert save, which persists the identifier of a transient instance, generates it in time, and returns the identifier, so it immediately executes the identifier.
5. The difference between saveOrUpdate,merge and update
Compare update and merge
The function of update is mentioned above, and here is the function of merge.
If there is an instance of the same persistence ID (identifier) in session, overwrite the existing persistent instance of session with the object given by the user
(1) when we use update, an exception will be thrown after the execution is completed.
(2) but when we use merge, we copy the property of the po object A that deals with the free state into the property of the po in the persistent state in the session, and after the execution is completed, it turns out whether it is in the persistent state or persistent state, and the A we provide is still in the free state.
VI. The difference between flush and update
The difference between the two is easy to understand.
Update operates on objects / / updateSQL in free or out-of-pipe state (out-of-pipe state due to session shutdown)
Flush, on the other hand, is an operational object in a persistent state.
By default, changes to a persistent object (including the set container) do not require update, as long as you change the value of the object and wait for hibernate flush to be automatically updated or saved to the database. Hibernate flush occurs in the following situations:
1. Call some queries and manual flush (). Close session and close SessionFactory.
Get () an object that changes the properties of the object and closes the resource.
2 the time of the transaction commit (including flush)
VII. The difference between lock and update
Update is to make an object in an unmanaged state that has been changed into a persistent state.
Lock is to change an object that has not changed its out-of-pipe state into a persistent state (for po objects (2) that are in the out-of-pipe state due to the shutdown of Session, but not for po objects that are out-of-pipe because of delete)
The operations of the two are different in response to changing the contents of a record:
The steps for update are:
(1) Modification of unmanaged objects after attribute changes-> call update
The steps for lock are:
(2) call lock to change an unmodified object from an unmanaged state to a persistent state-- > change the contents of an object in a persistent state-- > wait for flush or manual flush
VIII. The difference between clear and evcit
Clear completely clears the session cache
Evcit (obj) clears a persistent object from the session cache.
Session.lock (xtyhb,LockMode.NONE); / / means to go directly to the cache to find the object that has become persistent.
Session.lock (xtyhb,LockMode.READ); / / first read the ID of the record in the database through ID to see if there is such a record, and then go to the cache to find the object that has become persistent.
In Hibernate, objects have three states: temporary state, persistent state, and free state.
Also known as: transient state (Transient), persistent state (Persistent), unmanaged state (Detached). Objects in a persistent state are also called PO (Persistence Object), and transient objects and out-of-pipe objects are also called VO (Value Object).
Temporary state: when an entity object is new, the object is in a temporary state, that is, the object is only an area of memory that stores temporary data, and if no variable references the object, it will be reclaimed by the jre garbage collection machine. The data stored by this object has nothing to do with the database, and it will not be converted to a persistent object unless the temporary object is associated with the database through Session's save or SaveOrUpdate, and the data is inserted or updated into the database.
For example: Emp e=new Emp (); / / create a temporary object
E.setEmpno ((long) 8888)
E.setEName ("mike")
...
EmpDAO d=new EmpDAO ()
D.save (e); / / persistence
...
Persistent state: an instance of a persistent object has a corresponding record in the database and has a persistent representation (ID). After the delete operation on the persistent object, the corresponding record in the database will be deleted, then there is no corresponding relationship between the persistent object and the database record, and the persistent object becomes temporary.
After the persistent object is modified and changed, it will not be synchronized to the database immediately until the database transaction is committed. The persisted object is Dirty before synchronization.
For example:
Emp e=new Emp (); / / created a temporary object
EmpDAO edao= new empDAO ()
E=edao.findbyEmpno ((long) 7786); / / make objects correspond to database records, thus persisting
E.setEname ("new name"); / / modified the persistent object so that it is in Dirty
.
Edao.saveorupdate (e); / / Save, but still Dirty
Tran.commit (); / / submit, synchronize with the database and no longer Dirty
.
Free state: when Session Close, Clear or evict, the persistent object has the persistence identifier and the same value as the corresponding record in the database, but because the session has disappeared, the object is not in the persistence management, so it is in the free state (also known as: out-of-pipe state). An object in a free state is very similar to a temporary state object, except that it also contains a persistence identity.
Transient state
Java objects that open up memory space by the new command
Eg. Person person = new Person ("xxx", "xx")
If there is no variable to reference the object, it will be recycled by the java virtual machine.
The instantaneous object exists in memory in isolation. It is the carrier carrying information and does not have any relationship with the data of the database. In Hibernate, the instantaneous object can be associated with the database through the save () or saveOrUpdate () method of session, and the corresponding data can be inserted into the database. At this time, the instantaneous object is transformed into a persistent object.
Persistent state
Objects in this state have a corresponding record in the database and have a persistence identity. If you use the delete () method of hibernate, the corresponding persistent object becomes an instantaneous object, and because the corresponding data in the database has been deleted, the object is no longer associated with the database record.
When a session executes close () or clear (), evict (), the persistent object becomes an unmanaged object, and the persistent object becomes an unmanaged object. Although the object has a database identification value, it is no longer under the management of the HIbernate persistence layer.
Persistent objects have the following characteristics:
1. Associated with a session instance
two。 There are records associated with it in the database.
Out-of-tube state
When the session associated with a persistent object is turned off, the persistent object is converted to an unmanaged object. When an out-of-pipe object is re-associated to the session, it is transformed into a persistent object again.
The detached object has the identification value of the database, which can be transformed into persistent objects by update (), saveOrUpdate () and other methods.
Out-of-pipe objects have the following characteristics:
1. Essentially the same as an instantaneous object, JVM will recycle it at the appropriate time when no variable references it
two。 There is one more database record identification value than the instantaneous object.
About which three states of objects in hibernate are shared here, I hope the above content can be of some help to you and learn more knowledge. If you think the article is good, you can share it for more people to see.
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.