The difference between save (), update (), Saveorupdate () in Hibernate

Source: Internet
Author: User

The Save () method is obviously a save operation, and if you are saving a new newly-created object, you naturally use this method, which is not in the database.

Update () If you are updating a managed object that already exists, you must use the update () method, which is the object in the data.

Saveorupdate () This method is either updated or inserted, the primary key executes the update, and the insert is executed without a primary key.

Difference: For an object from a managed state to a transient (for an object that is removed from the database), the object itself has a primary key, but because it is deleted, this time because the record is not available in the database. But it also has a primary key exists, so this time can not use Update () or saveorupdate (), because the update () method is believed to have this record in the database, and saveorupdate is the execution of the first to see whether this object has a primary key, With the primary key then the update () method is executed, the Save () method is executed without the primary key, so the result is the same as the call to the update () method, and the result is an error because the object has been deleted and the record is not already in the database. It's just that it has a primary key (just in memory), so this is the Save () method.

The difference between save (), update (), Saveorupdate () in Hibernate

Related Article

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.