The difference between hibernate's update, merge, and Saveorupdate

Source: Internet
Author: User

1, first saveorupdate return void that is nothing to return and merge will return an object

2, the next saveorupdate is: Automatically determine whether the object has been persistent, if it has been persisted using the update operation or save operation, merge after executing the Session.merge (a) code, the A object is still not persisted state, A object will still not be associated to the session and update will persist the object

1. When there are null attributes in the Pojo object, use merge to not update the database and use Saveorupdate to update the database.

WORKAROUND: Do not update when you use Saveorupdate when the partial property is null

(1) Add a property to the profile: Update= "false", for situations that are never updated, such as Creattime.

Typically the following scenario uses update () or saveorupdate ():

    • The program loads an object in the first session
    • The object is passed to the presentation layer
    • There have been some changes to the object
    • The object is returned to the business logic layer
    • The program calls the update () method of the second session to persist these changes


Saveorupdate () do the following:

    • If the object is already persisted in this session, do nothing
    • Throws an exception if another object associated with this session has the same persistent identity (identifier)
    • If the object does not have a persisted identity (identifier) attribute, call Save () on it
    • If the persistent identity of the object (identifier) indicates that it is a newly instantiated object, call Save () on it
    • If the object is accompanied by version information (through <version> or <timestamp>) and the value of the Version property indicates that it is a newly instantiated object, save () it.
    • Otherwise, the update () object



Merge () can be very different:

    • If an instance of the same persistence identity (identifier) exists in the session, overwriting the old persisted instance with the state of the object given by the user
    • If the session does not have a corresponding persistent instance, try to load from the database or create a new persisted instance
    • Finally returns the persisted instance
    • User-given object is not associated with the session, it is still off the tube



Update and Merge methods, example
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 out the following logs:
Hibernate:select person0_.id as id0_0_, person0_.name as name0_0_ from person person0_ where person0_.id=?
The update action is less than the first case here

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

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

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: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=?

Saveorupdate (object): Hibernate automatically detects whether an object is transient or off-state, and then executes an INSERT or UPDATE statement
Merge (object): Similar to Saveorupdate, the difference is that after the Saveorupdate method operation, the state of the object is changed to a persistent state, and the merge is then off-state
In the mapping file <id name= "id" unsaved-value= "" >unsaved-value can set criteria for judging state

The difference between hibernate's update, merge, and Saveorupdate (RPM)

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.