Java Hibernate Save () differs from persist () _java

Source: Internet
Author: User

Hibernate provides a persist () approach that is almost exactly like the Save () feature, on the one hand, to take care of JPA usage habits. On the other hand, there is a difference between the save () and Persist () methods: When a persisted object is saved by using the Save () method, the method returns the identity property value of the persisted object (that is, the primary key value of the corresponding record), but when the persisted object is saved using the persist The method does not have any return values. Because the Save () method needs to return the identity property of the persisted object immediately, program execution Save () inserts the data for the persisted object into the database immediately, while persist () guarantees that when it is invoked outside of a thing, it is not immediately converted to an INSERT statement. This function is very useful, especially when we encapsulate a long session process, the persist () method is particularly important.

Here is a clear distinction. (Can follow SRC look, although the implementation steps are similar, but there are subtle differences)
Main content difference:
1,persist a transient instance is persisted, but the "not guaranteed" identifier (the attribute corresponding to the identifier primary key) is immediately filled into the persisted instance, and the filling of the identifier may be deferred to the flush.
2,save, which takes a transient instance to persist the identifier, produces it in time, and it returns an identifier, so it immediately executes the SQL insert

Other user's explanation:


Save method

When a persisted object is saved, the method returns the value of the identity property (that is, the primary key) of the persisted object.
This method immediately inserts the corresponding data for the persisted object into the database.

Persist method

The method does not return any values.

Guarantees that when it is invoked outside of a transaction, it is not immediately converted to an INSERT statement.
Suitable for long session flow.

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.