In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
What is the basis of Hibernate save? in view of this question, this article introduces in detail the corresponding analysis and solutions, hoping to help more partners who want to solve this problem to find a more simple and feasible way.
Hibernate has a lot to learn. Here we mainly introduce Hibernate save, including Hibernate saveOrUpdate and so on.
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 or free state (new DeptPo (1, "administrative department", 20, "administrative relevance"), the instance of the po is not associated with the session, and the instance of the po is in transient)
◆ persistent: persistent state (and the Po instance that you want to insinuate is recorded in the database, whose state is persistent, and the objects obtained through get, load, etc. Are all persistent)
◆ detached: out-of-tube 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.
The difference between Hibernate save and update
The reason for putting this pair in the * position is because this pair is the most commonly used.
The function of ◆ save is to save a new object
◆ update updates an out-of-control object or a free-state object (which must correspond to a record) to the database
The difference between Hibernate saveOrUpdate and update
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 objects in * 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 eventually to the persistence layer
The ◆ program creates a second session and calls the update () method of the second session to persist these changes.
Hibernate saveOrUpdate (po) does the following:
◆ if the po object has been persisted in this session, executing saveOrUpdate in this session does nothing
◆ throws an exception if savaOrUpdate (new po) has the same persistence identity (identifier) as another po object associated with this session
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
The difference between Hibernate save and persist
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.
This is the answer to the question about what is the basis of Hibernate save. I hope the above content can be of some help to you. If you still have a lot of doubts to be solved, you can follow the industry information channel to learn more about it.
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.