The difference between Hibernate merge and update

Source: Internet
Author: User

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

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.