Difference between save() and persist() methods in hibernate: Using Hibernate framework to save object we mostly use two methods save() and persist(). Both methods fulfill the same purpose.
What is the difference between save() and persist() methods in Hibernate? This is the common interview question and asked most of the time by the interviewer. There is no big difference between save and persist method.
The important differences between persist() and save() methods are:
- The common difference between
save() and persist() method is from
it's definition, The return type of
persist() method is void whereas
save() method is of type
Serializable object.
- Both save() and persist() methods
are used to save the transient instance. The save() method assigned the identifier value immediately to the persistent object while persist() not guarantee.
- The persist() method will not
execute outside the transaction
boundaries, whereas save() can run
from inside or outside the
transaction boundaries.
Conclusion :
The main difference between the save() and persist() method is that we can get the identifier value using save() method where as persist() can not .
long s = session.save(student);
The above line will save the student object and return the id generated for this object.
0 Comment(s)