Difference between Hibernate save () and persist ()

Source: Internet
Author: User

Hibernate provides a persist () method that is almost similar to the save () function. On the one hand, it aims to take care of the usage habits of JPA. On the other hand, there is another difference between the save () and persist () methods: when you use the save () method to save a persistent object, this method returns the identifier property value of the persistence object (that is, the primary key value of the corresponding record). However, when the persist () method is used to save the persistence object, this method does not return any value. Because the save () method needs to immediately return the identifier attribute of the persistent object, the program executes save () to immediately insert the data corresponding to the persistent object into the database; and persist () this function is useful to ensure that it is not immediately converted into an insert statement when called outside a transaction. Especially when we encapsulate a long session flow, persist () method is particularly important.


A clear distinction is given here. (You can follow up with src and check that although the implementation steps are similar, there are still slight differences)

Main differences:
1. persist persists a transient instance, but the identifier (the attribute corresponding to the identifier primary key) is not guaranteed to be immediately entered into the persistence instance, enter identifiers may be postponed to flush.

2. save: generate a transient instance persistence identifier in a timely manner. It returns the identifier, so it will immediately execute SQL insert

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.