• Register

Share:- Whatsapp Facebook Facebook

------------------------------------- Welcome to Developerhelpway Q&A, where you can ask questions and receive answers from other members of the community.

Categories

0 votes
1.9k views
What is the difference between save, persist and saveOrUpdate methods in Hibernate?
in Hibernate by

1 Answer

0 votes

save():
1) The save() method generates a new identifier and INSERT record into database.
2) This method saves records into database by generating INSERT SQL query.
3) It generates a new identifier and return the Serializable identifier back.
4) It returns a Serializable object.
5) The save() method in Hibernate is that is there behavior on outside of transaction boundaries.

saveOrUpdate():
1) The saveOrUpdate() method can either INSERT or UPDATE records based upon existence.
2) The saveOrUpdate is more flexible to use but it involves an extra processing to find whether record is already exists in table or not.
3) The saveOrUpdate() method either INSERT or UPDATE records based upon existence of object in database. If persistence object is already exists in database then UPDATE SQL will execute and if do not found object in database than INSERT will run.

persist():
1) The persist() method is similar to save() method, it also INSERT records into database but return type of persist is void.
2) The persist() method doesn't guarantee that the identifier value will be assigned to the persistent instance immediately, the assignment might happen at the time of flushing.
3) The persist() method guarantees, it will not execute an INSERT statement when you called outside of transaction boundaries.

by
...