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