The difference between hibernate's merge and update method

Source: Internet
Author: User

The

did a test today and wrote a test case to see what the difference is between the log that the console prints when the merge is in and the update. The entity bean is simple, with ID and name two fields, and then the console log content of the following test scenarios:

1. The database record already exists, and the name of the person is changed to a new name. The

Merge method prints the following log:
Hibernate:select person0_.id as id0_0_, person0_.name as name0_0_ from person person0_ where Person0_.id=?
Hibernate:update person set name=? where id=? The

Update method prints the following log:
Hibernate:update person set name=? where id=?

2. The database record already exists, changing the name of person and the same value as the name of the ID record in the database. The

Merge method prints the following log:
Hibernate:select person0_.id as id0_0_, person0_.name as name0_0_ from person person0_ where Person0_.id=?
The
Update method prints the following log:
Hibernate:update person set name=? where id=?

3. When a database record does not exist, the ID of the entity bean that you passed does not have a corresponding record in the database. The

Merge method prints the following log:
Hibernate:select person0_.id as id0_0_, person0_.name as name0_0_ from person person0_ where Person0_.id=?
Hibernate:insert into (name) values (?)

If there is no corresponding record, the merge will insert the record as a new record. I am puzzled here because I passed the ID value of the person entity object, why does it still do the insertion action?

Online friend's answer: because there is no way to find the object can not be matched, cannot be modified, to the database to re-insert a piece of data

The Update method prints out the following log:
Hibernate:update person set name=? where id=?
2009-11-22 20:59:55,359 ERROR [org.hibernate.jdbc.AbstractBatcher]-Exception executing batch:
Org.hibernate.StaleStateException:Batch Update returned unexpected row count from Update [0]; Actual row count:0; Expected:1

The following are excerpts from the Web:
When we use update, the state of the object A that we provide becomes persisted after execution is complete.

But when we use the merge, the execution is done, we provide the object A is still out of the tube State, hibernate or new a B, or retrieve a persistent object B, and we provide a copy of all the values of object A to this B, after the completion of the implementation of B is a persistent state, And the A we provide is a managed state.


Summary of Friends Online:

The merge (Oldobj) operation is as follows:

if (oldobj no ID) {
Add action
} else {
If (object not found in hibernate context according to Oldobj.id) {
Loading gets newobj;
if (NEWOBJ = = null) {
Add action
}

}else{
NewObj
}

If (Attributes inside Oldobj and newobj are inconsistent) {
Update action
}


}

Three states of Hibernate objects in Hibernate reference books

Hibernate defines and supports the following object States (state):

  • instantaneous (Transient) -The new object created by the operator and not yet associated with Hibernate Session is considered instantaneous (Transient). Instantaneous (Transient) objects are not persisted to the database and are not given persistent identities (identifier). If an instantaneous (Transient) object is not referenced in the program, it is destroyed by the garbage collector (garbage collector). Hibernate can be used to turn Session it into a persistent (persistent) state. (Hibernate will automatically execute the necessary SQL statements)

  • Persistent (persistent) -persistent (persistent) instances have corresponding records in the database and have a persistent identity (identifier). An instance of persistence (persistent) may have just been saved, or just loaded, regardless of which, by definition, exists within the associated scope Session . Hibernate detects any changes to an object that is in a persistent (persistent) State and synchronizes the object data (state) with the database (synchronize) when the current operating unit (unit of work) finishes executing. Developers do not need to do it manually UPDATE . Changing an object from a persistent (persistent) state to an instantaneous (Transient) state also does not require manual execution of DELETE statements.

  • off-pipe (Detached) -The Session object becomes off-pipe (Detached) when it is closed with a persistent (persistent) object. The reference to the Detached object is still valid and the object can continue to be modified. If the Detached object is re-associated to a new one Session , it will be converted again to persistent (persistent) (changes in the Detached will be persisted to the database). This feature makes it possible for a programming model to be programmed with a long-running operating unit (unit of work) in which the user thinks time is think-time. We call it an application transaction , that is, from the user's point of view, an operating unit (unit of work).

The difference between hibernate's merge and update method

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.