Hibernate in Saveorupdate (), Save (), update (), merge () What do you think of it;

Source: Internet
Author: User

The idea of MyBatis hibernate was abandoned.

Anyway, the stickers are first. Hibernate three status bits and switching between the use of functions;


Summarize the functions of saveorupdate (), Save (), update (), merge ().

Save (), update (): It's all very well understood.

Save () generates a record in the database and, if there is one in the database, an error message that there is a duplicate record.
Update () is the updating of records in the database;

Merge () is very wonderful:
1. If there is an instance of the same persistence identifier (identifier) in the session, overwrite the old persistent instance with the state of the object given by the user;
2. If the session does not have a corresponding persistent instance, attempt to load from the database or create a new persisted instance;
3. Finally returns the persistent instance;
4. The user gives the object is not linked to the session, it is still off the tube;

Merge () Reference: http://blog.csdn.net/lang_man_xing/article/details/7572964


Saveorupdate (): Can be above the picture here in a word. a deeper understanding is that Java object-oriented is all aspects .

Temporary object, that is, not in the session, there is no persistent identity, the database has no corresponding record of this data, at this time to use save;
Persistent object, in the session, has persistent identity OID, at this time the contents of the data as long as the modification, (ID) can not be changed, will cause the data this object and database linkage,
This is done by hibernate through synchronization. Specifically how to achieve, can refer to the life cycle of objects, and garbage collection has a trace of the relationship. The data at this point can be modified even if you do not use Saveorupdate to update the database.
There is also an object: a de-tube object. This object has a persistent identity, which means that the database has the corresponding data record, but the session is closed, that is, the session cache does not have this object. Not subject to the management of the session. If you modify the properties of this object, there will be no linkage,
That is, the data is not reflected in the database, unless the Saveorupdate () method is called, and after commit, the object becomes a persisted object.


Hibernate in Saveorupdate (), Save (), update (), merge () What do you think of it;

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.