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.