Detailed description of update and saveorupdate

Source: Internet
Author: User
Concepts:

In hibernate, the core concept is po state management. 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: JavaCode

    1. Foo = sess. Load (FOO.Class, ID );;
    2. Foo. setxxx (XXX );;
    3. Sess. Flush ();;
    4. Sess. Commit ();;
 
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
  1. // In the first session
  2. Cat cat = (CAT); firstsession. Load (cat.Class, Catid );;
  3. Cat potentialmate =NewCAT ();;
  4. Firstsession. Save (potentialmate );;
  5. // In a higher tier of the application
  6. Cat. setmate (potentialmate );;
  7. // Later, in a new session
  8. Secondsession. Update (CAT );;// Update cat
  9. 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

    1. Public void addmate (Cat cat, mate); {
    2. session =...;
    3. transacton Tx =...;
    4. session. Update (CAT);
    5. CAT. addmate (mate);
    6. Tx. Commit ();
    7. session. Close ();
    8. };
Public void addmate (CAT, mate); {session = ...; transacton Tx = ...; session. update (CAT); cat. addmate (mate); Tx. commit (); Session. close ();;};

Obviously, you need to encapsulate hibernate operations in Dao so that the Business LayerProgramProgrammers and web programmers do not need to understand Hibernate and call Dao directly.

At this point, the problem arises: The above code runs correctly and there is a necessary premise, that is, the cat object of the method call parameter must be a persistent Po, that is, it should be first queried from the database before it can be used in this way. But the programmer at the business layer obviously does not know this kind of internal mystery. If his business is to add a cat and then add its mate, he will obviously call it like this, after a new cat object is created, addmate:

Java code
    1. Cat cat =NewCAT ();;
    2. Cat. setxxx ();;
    3. Daoimpl. addmate (CAT, mate );;
 
Cat cat = new CAT (); CAT. setxxx (); daoimpl. addmate (CAT, mate );;

However, please note that this cat object is only a Vo, it has not been persisted, it is not a po, It is not qualified to call the addmate method, therefore, calling the addmate method does not actually send an update SQL statement to the database. This cat object must be saved to the database first. Only after it becomes a PO can it be eligible for addmate.

You must perform the following operations:

Java code
    1. Cat cat =NewCAT ();;
    2. Cat. setxxx ();;
    3. Daoimpl. addcat (CAT );;
    4. Daoimpl. addmate (CAT, mate );;
 
Cat cat = new CAT (); CAT. setxxx (); daoimpl. addcat (CAT); daoimpl. addmate (CAT, mate );;

The cat is persistent before other persistent operations can be performed on the cat. Therefore, programmers at the business layer must be clear about the state of the cat object, whether it is the first or the third. If it is the first type, you must first save and then addmate; if it is the third type, you can directly addmate.

But the most critical thing is that if there are many layers of software, the programmer at the business layer may also get this cat object from the upper web application layer, he does not know whether the cat is Vo, whether it has not been persisted, or whether it has been persisted, so he has no way to write a program.

Therefore, such daoimpl is obviously problematic. It will cause many programming traps for programmers at the business layer, the business-layer programmer must have a profound understanding of the State management of the Po object for each DAO he calls, and must have a profound understanding of the exact state of his PO object at any time, in order to ensure the correctness of programming, this is obviously not possible, but with saveorupdate, these problems can be solved.

Now you need to modify the addmate method:

Java code
    1. Public VoidAddmate (CAT, mate );{
    2. Session session = ...;
    3. Transacton Tx = ...;
    4. Session. saveorupdate (CAT );;
    5. Cat. addmate (mate );;
    6. TX. Commit ();;
    7. Session. Close ();;
    8. };
Public void addmate (CAT, mate); {session = ...; transacton Tx = ...; session. saveorupdate (CAT); cat. addmate (mate); Tx. commit (); Session. close ();;};

As shown above, if the programmer at the business layer transmits a persistent Po object, Hibernate updates the cat object (assuming that the programmer at the business layer modifies the cat attribute outside the session ), if a new object is input, save the Po object to the database.

BTW: whether hibernate updates the cat object or saves the cat object depends on the setting of unsave-value.

In this way, programmers at the business layer do not have to worry about the Po status. For them, whether cat is a new object, it is just a vo; or the Po objects queried from the database are all directly addmate and OK:

Java code
    1. Daoimple. addmate (CAT, mate );;
 
Daoimple. addmate (CAT, mate );;

This is the role of saveorupdate.

Statement: javaeye Article Copyright belongs to the author and is protected by law. You shall not reprint this document without the written consent of the author.

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.