In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly introduces "what are the differences in the preservation of Hibernate". In the daily operation, I believe that many people have doubts about the differences in the preservation of Hibernate. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the doubts about "what are the differences in the preservation of Hibernate?" Next, please follow the editor to study!
I. preliminary knowledge
For Hibernate, its objects have three states, transient, persistent, and detached
The following are common translation methods:
Transient: transient or free 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 disabled, the po object of persistent in the session cache also becomes detached. Those that become free due to closing session can become persistent through lock, save and update, and persistent instances can be changed into unmanaged state 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. INSERT,delete () will raise SQLDELETE, while update () or merge () will raise 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.
With these preliminary knowledge, you can wake up and compare different Hibernate preservation methods.
II. The difference between save and update
The reason for putting this pair in the * position is because this pair is the most commonly used. The purpose of save is to save a new object. Update is to update an object in an unmanaged state 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 basically combines save and update, while update simply quotes a passage from Hibernate reference to explain their usage and differences. Usually, the following scenarios use update () or saveOrUpdate ():
The program loads objects in * session, and then closes 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 the functions of update and merge,update. Let's talk about merge here. 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, copy the property of the po object A that deals with the free state to the property of the po that is in the persistent state in the session, and the execution is completed whether the original state is persistent or persistent, and the A we provide is still free.
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 query and manual flush (), session close, SessionFactory close combined with get () an object, change the properties of the object, close the resource.
2 the time of the transaction commit (including flush)
VII. The difference between lock and update
Update is to change an object that has been changed from an unmanaged state to a persistent state, and lock is to change an object that has not been changed into a persistent state (for po objects (2) that are in an unmanaged state due to the shutdown of Session, but not for po objects that are in an unmanaged state 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.
At this point, the study on "what are the differences in the preservation of Hibernate" 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.