The difference between update and saveorupdate in Hibernate

Source: Internet
Author: User

[difference between update and saveorupdate in turn]hibernateCategory: Hibernate summary 2009-11-17 00:04 2121 People read comments (0) favorite reports Hibernatesession Database Javadaoapplication

Let's start with a little concept:

In Hibernate, the core concept is the state management of the PO. There are three statuses for a PO:

1. Vo that has not been persisted
This is a memory object Vo, managed by the JVM lifecycle

2. The PO that has been persisted, and within the session life cycle
Database data is mapped at this time, and the life cycle is managed by the database

3, has been persistent, but now and the session has been detached, to VO's identity in the run
This and the session has been detached the PO can also enter another session, continue to PO state management, at this time it becomes the second state of PO. This PO is actually a cross

The session is maintained in a state.

In the traditional jdo1.x, PO only the first two states, a PO once out of the PM, lost the state, no longer associated with database data, become a pure memory vo, it even into the

into a new PM, nor can it restore its state.

Hibernate is a strong place, after a PO out of session, but also to maintain the state, and then enter a new session, the ability to restore state management, but at this time the state management

Need to use session.update or session.saveorupdate, this is the "requires a slightly different programming" mentioned in Hibernate reference

Model "

Now formally enter this topic:

In a nutshell, update and saveorupdate are used to manage the state of the PO that crosses the session.

Assuming that your PO does not need to cross the session, then you do not need to use, for example, you open a session, the PO operation, and then close, and then this PO you will not be used again,

Then you don't need to use update.

So let's look at the 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 ();


Po Object foo is done in a session life cycle, so you do not need to explicitly do Sess.update (foo) operations. Hibernate automatically detects that the Foo object has been

has been modified so that an update of SQL is sent to the database. Of course if you have to add Sess.update (foo) is not wrong, but it is not necessary to do so.

and the cross-session means that this PO object after the session is closed, you also use it as a VO, and then you modify its properties outside the session, and then you want to play

Open a session, the properties of VO to save the database inside, then you need to use the 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


The cat and mate objects were taken in the first session, and after the first session was closed, they became the third State of the PO, and the PO that the session had detached, when he

Their status information is still preserved. When they enter the second session, they can immediately update their status. However, due to modifications to the cat operation: cat.setmate

(potentialmate); is performed outside the session, Hibernate cannot know that the Cat object has been altered, and the second session is not aware of this modification, so be sure to explicitly

The call to Secondsession.update (cat); Notifies the hibernate,cat that the object has been modified, and you must send the update SQL.

So this is what the update does, and it only needs to be written when a PO object is synchronized across the session. and a PO object when it does not need to cross the session to form

It is not necessary to write the update when the configuration is managed.

Let's talk about Saveorupdate's useful:

The difference between saveorupdate and update is the strategy that hibernate takes with the PO in the cross-session PO State management.

For example, when you write a daoimpl, let the Cat object add a mate, 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 (); };


Obviously you need to encapsulate the hibernate operation in DAO, so that the business layer programmer and the Web layer programmer do not need to know hibernate to invoke the DAO directly.

At this point, the problem is: The above code runs correctly there is a necessary premise, that is, the method call parameter cat object must be a has been persisted po, that is, it should

First, it is queried from the database before it can be used. But the business-level programmer obviously does not know this kind of internal mysticism, if his business is now adding a cat, and then adding

It's mate and he's obviously going to call that new a cat object come out and then just addmate:

Java code
Cat cat = new Cat ();
Cat.setxxx ();
Daoimpl.addmate (cat,mate);

Cat cat = new Cat (); Cat.setxxx (); Daoimpl.addmate (cat,mate);


But please note that this cat object is just a VO, it is not persisted, it is not a PO, it is not eligible to call the Addmate method, so calling the Addmate method does not really go to

In the database to send the update SQL, this cat object must first be saved to the database, after really become a PO, only to have addmate eligibility.

You have to do this:

Java code
Cat cat = new Cat ();
Cat.setxxx ();
Daoimpl.addcat (cat);;
Daoimpl.addmate (cat, mate);;

Cat cat = new Cat (); Cat.setxxx (); Daoimpl.addcat (cat);; Daoimpl.addmate (cat, mate);;


The cat is persisted before other persistent operations can be performed on the cat. Therefore, the programmer of the business layer must be aware of what state the Cat object is in, whether it is the first or the third.

If it is the first, save first, then addmate, if it is the third, it will be addmate directly.

But the most deadly is that if the whole software layer is a lot, the business level of the programmer he got this cat object may be the upper Web application layer passed over the cat, he does not know this

Whether Cat is VO, has not been persisted, or has been persisted, then there is no way he can write the program.

So the Daoimpl is obviously problematic, and it creates a lot of programming pitfalls for programmers in the business layer, and the business layer programmer must have a deep understanding of every DAO pair PO pair that he calls

Like what kind of state management, must have a deep understanding of his Po object at any time in what exact state, in order to ensure the correctness of programming, obviously this is not possible, but with

Saveorupdate, these problems will be solved.

Now you need to modify the Addmate method:

Java code
public void Addmate (cat cat, mate mate); {
Session session = ...;
Transacton tx = ...;
Session.saveorupdate (cat);;
Cat.addmate (mate);;
Tx.commit ();
Session.close ();
};

public void Addmate (cat cat, mate mate);     {Session session = ...;     Transacton tx = ...; Session.saveorupdate

(cat);;     Cat.addmate (mate);;     Tx.commit (); Session.close (); };


As above, hibernate updates the Cat object if the business layer programmer passes in an already persisted PO object (assuming that the business layer programmer modifies it outside the session

Cat's properties), save this PO object to the database if it is passed in as a new object.

Btw:hibernate whether to update the Cat object or the Save Cat object at this time depends on the unsave-value settings.

In this way, the business layer of the programmer will not have to worry about the status of Po, for them, whether cat is new out of the object, just a VO, or from the database query

PO objects, all of them are directly addmate OK:

Java code
Daoimple.addmate (cat, mate);;

Daoimple.addmate (cat, mate);;


This is the role of saveorupdate.

The difference between update and saveorupdate in Hibernate

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.