Update & Savaorupdate [Author: Robbine Resource: Javaeye]

xiaoxiao2021-03-06  71

First point concept: In Hibernate, the most core concept is to manage the status of PO. One PO has three states: 1, the unopened Vo is a memory object Vo, manages lifecycle 2 by JVM, has been persisted, and the database data is mapped at this time in the session lifecycle, by the database Manage life cycle 3, it has been persisted, but now and session have detached, with VO's identity running this and session already detached PO can also enter another session, continue PO status management, at this time it Become a second state of PO. This type of PO is actually maintained by SESSION. In the traditional JDO1.x, the PO only has two states. Once a PO is separated from PM, it is lost, it is no longer associated with the database data, and becomes a pure memory Vo, which even enters a new PM, Can't restore its state. Hibernate is in that, after a PO is separated from session, it can also keep a state. After entering a new session, restore status management capabilities, but at this time, the status management needs to use session.Update or session.saveorupdate, this is "Requires a Slightly Different Programming Model" mentioned in Hibernate Reference officially enters this topic: Simple, Update and SaveorUpdate are used to manage PO across session. Suppose your PO does not need to cross the session, then you don't need to use, for example, you open a session, do it, then turn it off, then this PO you will not use it, then you don't need to use Update. So let's take a look at the above example:

Java code:

1 foo foo = sess.

Load

Foo.

Class, ID

);

2 foo.

Setxxx

(xxx

);

3 sess.

Flush

(

);

4 sess.

Commit

(

);

The operation of the PO object FOO is completed within a session life cycle, so there is no need to explicitly perform SESS.UPDATE (FOO) operations. Hibernate automatically monitors the Foo object has been modified, so send an Update SQL to the database. Of course, if you have to add sess.update (foo), it will not be wrong, but there is no need to do this. And the meaning of session is that this PO object has been used as a VO after session, and then you have modified its properties outside, then you want to open a session, modify the property of VO Save to the database, then you need Update.

Java code:

1

// in The First Session

2 Cat cat =

(Cat

FigSTSession.

Load

(CAT.

Class, Catid

);

3 cat potentialmate =

New cat

(

);

4 firstession.

Save

(Potentialmate

);

5

6

// in a higher tier of the application

7 cat.

SetMate

(Potentialmate

);

8

9

// Later, in a new session

10 secondsession.

Update

(cat

);

// Update Cat

11 secondsession.

Update

(Mate)

);

// Update Mate

12

Cat and Mate objects are made in the first session. After the first session is closed, they become the third state of PO, and the session already detached PO, at this time their status information is still retained . When they entered the second session, they can be updated immediately. However, due to the modification of CAT: Cat.SetMate (PotentialMate); is outside the session, Hibernate can't know that the Cat object has been changed, the second session does not know this modification, so it must explicitly Call SecondSession.Update (CAT); notify hibernate, the Cat object has been modified, you must send Update's SQL. So the role of Update is here, it will only be used to write when a PO object is synchronized across the session. And a PO object does not need to write Update when it does not need to manage state management across the session. Talking about SaveorUpdate: SaveorUpdate and Update are the difference between Hibernate to PO in PO status management in PO status in the SESSION. For example, when you write a DaoIMPL, let the CAT object add a MATE, as follows:

Java code:

1

public

Void Addmate

(Cat Cat, Mate Mate

)

{

...}

2 session session = ...;

3 Transacton TX = ...

4 session.

Update

(cat

);

5 cat.

Addmate

(Mate)

);

6 TX.

Commit

(

);

7 session.

Close

(

);

8

}

Obviously you need to encapsulate Hibernate's operation in Dao, so that the programmer of the business layer and the web layer does not need to know Hibernate, call the DAO. At this time, the problem is coming: The above code is running correctly, that is, the method calling the parameter Cat object must be a PO that has been persisted, that is, it should first query from the database, then Can use this way. But the programmer of the business layer obviously doesn't know this internal mystery. If his business is now adding a cat, then add its MATE, he obviously calls, New a cat object, then addmate:

Java code:

1 Cat Cat =

New cat

(

);

2 CAT.

Setxxx

(

);

3 DaoIMPL.

Addmate

(Cat, Mate

);

But please note, this cat object is just a Vo, it is not persisted, it is not PO, it is not eligible to call the Addmate method, so call the addmate method to send Update SQL to the database, this CAT object must First by SAVE to the database, you have the qualifications for addMate after you really become a PO. You have to do this:

Java code:

1 Cat Cat =

New cat

(

);

2 CAT.

Setxxx

(

);

3 DaoIMPL.

Addcat

(cat

);

4 DaoImpl.

Addmate

Cat, Mate);

Popularize CAT first before you can do other persistent operations for CAT. Therefore, the programmer that requires the business layer must clear what state is in the CAT object, and the first is the first, or the third. If it is the first, you must first save, then addMate; if it is a third, you will directly addMate. But the most deadly is that if the entire software is slid, the programmer of the business layer, he got this CAT object, it might be the CAT passing through the upper web application layer. He doesn't know that this CAT is VO, not lasting. After it is, it has been persisted, then he has no way to write the program at all. So this DaoIMPL is obviously problematic. It will cause a lot of programmed traps on the business layer, and the programmer of the business layer must be profoundly understanding what the DAO he calls is to manage the PO object. You must understand what the PO object is in any exact state at any time, in order to ensure the correctness of the programming, obviously don't do it, but there is SaveorupDate, these questions are solved. Now you need to modify the Addmate method:

Java code:

1

public

Void Addmate

(Cat Cat, Mate Mate

)

{

...}

2 session session = ...;

3 Transacton TX = ...

4 session.

Saveorupdate

(cat

);

5 cat.

Addmate

(Mate)

);

6 TX.

Commit

(

);

7 session.

Close

(

);

8

}

As mentioned above, if the programmer of the business layer is coming in, it is a persisted PO object, then Hibernate updates the Cat object (assuming that the programmer of the business layer modifies the CAT attribute outside the session), if it is passed The new New's object, then to the database Save this PO object. BTW: Hibernate At this time, the CAT object is taken, or the Save Cat object depends on the setting of Unsave-Value. In this way, the programmer of the business layer does not have to worry about the status of PO. For them, regardless of CAT is the object coming out, it is just a VO; or the PO object from the database is also good, all It is OK that is directly addmate:

Java code:

1 DaoImple.

Addmate

(Cat, Mate

);

This is the role of saveorupdate.

转载请注明原文地址:https://www.9cbs.com/read-91950.html

New Post(0)