Hibernate save () saveorupdate () usage page 1/2

Source: Internet
Author: User

One PO has three statuses:
1. non-persistent VO
In this case, it is a memory object VO, which is managed by JVM.
2. A persistent PO within the Session Lifecycle
In this case, the database data is mapped and managed by the database.
3. I have been persistent, but now I have detached the Session and run it as a VO.
This kind of PO that has been detached with the Session can also enter another Session and continue PO state management. At this time, it becomes the second State of the PO. This type of PO is actually maintained across sessions.
In the traditional JDO1.x, only the first two States of a PO are available. Once a PO is detached from the PM, it is lost and no longer associated with the database data, making it a pure memory VO, even if it enters a new PM, it cannot be restored.
What makes Hibernate strong is that after a PO is detached from the Session, it can maintain the status and then enter a new Session to restore the State management capability. However, in this case, session is required for status management. update or session. saveOrUpdate, which is the "requires a slightly different programming model" mentioned in the Hibernate Reference"
This topic is now officially introduced:
In simple terms, update and saveOrUpdate are used to manage the status of pods across sessions.
Assume that your PO does not need to be used across sessions. For example, if you open a Session, perform operations on the PO, and close the PO, then you will not use this PO, therefore, update is not required.
Therefore, let's take a look at the previous example:
Java code
Foo foo = sess. load (Foo. class, id );;
Foo. setXXX (xxx );;
Sess. flush ();;
Sess. commit ();;

Foo foo = sess. load (Foo. class, id );;
Foo. setXXX (xxx );;
Sess. flush ();;
Sess. commit ();;
Foo foo = sess. load (Foo. class, id );;
Foo. setXXX (xxx );;
Sess. flush ();;
Sess. commit ();;
The operations on the PO object foo are completed within the lifecycle of a Session, so explicit operations such as sess. update (foo) are not required. Hibernate automatically detects that the foo object has been modified. Therefore, it sends an update SQL statement to the database. Of course, if you want to add sess. update (foo), it will not be wrong, but there is no need to do so.
Cross-Session means that after the Session is closed, you use the PO object as a VO. Later, you modify its attributes outside the Session, then you want to open a Session and save the modified VO attributes to the database. Then you need to use update.
Java code
// In the first session
Cat cat = (Cat); firstSession. load (Cat. class, catId );;
Cat potentialMate = new Cat ();;
FirstSession. save (potentialMate );;
// In a higher tier of the application
Cat. setMate (potentialMate );;
// Later, in a new session
SecondSession. update (cat); // update cat
SecondSession. update (mate); // update mate

// In the first session
Cat cat = (Cat); firstSession. load (Cat. class, catId );;
Cat potentialMate = new Cat ();;
FirstSession. save (potentialMate );;
// In a higher tier of the application
Cat. setMate (potentialMate );;
// Later, in a new session
SecondSession. update (cat); // update cat
SecondSession. update (mate); // update mate
// In the first session
Cat cat = (Cat); firstSession. load (Cat. class, catId );;
Cat potentialMate = new Cat ();;
FirstSession. save (potentialMate );;
// In a higher tier of the application
Cat. setMate (potentialMate );;
// Later, in a new session
SecondSession. update (cat); // update cat
SecondSession. update (mate); // update mate
The cat and mate objects are obtained in the first session. After the first session is closed, the cat and mate objects become the third state of the PO, and the PO with the Session already detached, their status information is retained. When they enter the second session, they can immediately update the status. But because of the cat Modification Operation: cat. setMate (potentialMate); is performed outside the Session. Hibernate cannot know that the cat object has been modified. The second Session does not know the modification. Therefore, it is necessary to explicitly call secondSession. update (cat); Notify Hibernate that the cat object has been modified. You must send the update SQL statement.
Therefore, update is used only when a PO object synchronizes status across sessions. A po object does not need to write update when it does not need to perform status management across sessions.
Let's talk about the use of saveOrUpdate:
The difference between saveOrUpdate and update lies in what policies Hibernate uses for PO in cross-Session PO state management.
For example, when you write a DAOImpl, add a mate to the cat object, as defined below:
Java code
Public void addMate (Cat cat, Mate mate );{
Session session = ...;
Transacton tx = ...;
Session. update (cat );;
Cat. addMate (mate );;
Tx. commit ();;
Session. close ();;
};

Public void addMate (Cat cat, Mate mate );{
Session session = ...;
Transacton tx = ...;
Session. update (cat );;
Cat. addMate (mate );;
Tx. commit ();;
Session. close ();;
};
Public void addMate (Cat cat, Mate mate );{
Session session = ...;
Transacton tx = ...;
Session. update (cat );;
Cat. addMate (mate );;
Tx. commit ();;
Session. close ();;
};
Obviously, you need to encapsulate Hibernate operations in DAO, so that programmers at the business layer and programmers at the Web layer do not need to understand Hibernate and directly call DAO.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.