Today I did a test and wrote a test case to see what the difference is between merge and the log that the console prints when the update was made. Entity beans are simple, with IDs and name two fields, and then the console log content for the following test scenarios, respectively:
1. Database record already exists, change person's name to a new name.
The merge method prints out the following logs:
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 out 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=? Here, the update method prints out the following number of actions that are missing from the first case: hibernate:update person set name
3.
When the database record does not exist, that is, the ID of the entity bean 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?
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 [
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 copies all the values of the object a we provide to this B, after execution completes B is a persistent state, and we provide a or managed state
The difference between Hibernate merge and update