Difference betwixt save(), saveOrUpdate() in addition to persist() inwards Hibernate Session

Session interface inward Hibernate provides a pair of methods to movement an object from novel or transient patch to persistent patch e.g. save(), saveOrUpdate() as well as persist() are used to shop an object into the database, exactly at that topographic point are about pregnant differences betwixt them. The Session.save() method does an INSERT to shop the object into the database as well as it besides render the identifier generated past times the database. On the other hand, saveOrUpdate() tin live on used to reattach a detached object inward Hibernate Session i.e. it tin create INSERT or UPDATE depending upon whether object exists inward database or not. The 3rd method, persist() is similar to Session.save() i.e. it is used to movement a transient object to the persistent patch past times storing it into the database exactly it doesn't render the database identifier. This was the basic departure betwixt save() vs saveOrUpdate() vs persist() method as well as advise when to role which method. It is besides i of the often asked Hibernate interview questions on Java Web developer chore interviews. Knowing this special assist you lot to create good on your interview. Btw, if you lot accept non read already as well as thus you lot must read Java Persistence amongst Hibernate, best mass to acquire these kinds of subtle details close Hibernate.



Session.save() vs Session.saveOrUpdate() vs Session.Persist

As I said, all 3 methods save(), persist() as well as saveOrUpdate belongs to Hibernate's Session interface. They all are used to convey a novel object into persistence context or a detached object, which was associated amongst a Hibernate Session inward past times exactly currently it's non associated into active persistence context. It is besides i of the most popular Hibernate interview questions. Let's come across pair of to a greater extent than differences betwixt save(), persist() as well as saveOrUpdate() methods of Session interface.



1) The primary departure between save() as well as saveOrUpdate() method is that save() method performs an INSERT functioning to shop the object into the database, exactly INSERT volition neglect if the primary key is already persistent i.e. object already exists inward the database. This is why, you lot should exclusively telephone band save() amongst an absolutely novel object which doesn't accept whatever database identifier. Calling save() amongst the detached object volition fail. This is contrary of saveOrUpdate() method, which tin create either INSERT or UPDATE SQL query depending upon whether an object exists inward the database or not. The saveOrUpdate() method showtime executes a SELECT query to decide if it needs to create an INSERT or UPDATE operation.


2) Another commutation departure betwixt save() as well as saveOrUpdate() method is that sometime is used to convey a transient object to persistent state exactly saveOurUpdate() tin convey both transient (new) as well as detached (existing) object into persistent state. It is often used to re-attach a detached object into Session.

Here is an object's lifecycle as well as patch transition diagram inward Hibernate which shows that save() as well as saveOrUpdate() tin movement an object from transient to persistent state.




3) Coming to persist() method, the departure betwixt save() as well as persist() method is that sometime returns the generated database identifier, a Serializable object exactly persist() method doesn't render anything. It's render type is void.

For example:
System.out.println(session.save(aCoin));

volition impress the generated primary key, exactly the next business volition throw a compile fourth dimension fault because persist()'s render type is void.

System.out.println(session.persist(aCoin));


That's all close difference betwixt save(), saveOrUpdate() as well as persist() method of Hibernate Session interface. Now you lot know the basic departure betwixt them as well as besides learned when to role the save(), saveOrUpdate() as well as persist() method. Use the save() method to shop novel object into database as well as when you lot demand the generated database identifier, otherwise role persist() method. You tin role saveOrUpdate() to reattach a detached object into Hibernate Session.

Further Learning
Introduction To Hibernate
Spring as well as Hibernate for Beginners
Hibernate Interview Questions Preparation Course

Thanks for reading this article, if you lot similar this article as well as interview interrogation as well as thus delight portion amongst your friends as well as colleagues. If you lot accept whatever interrogation or feedback as well as thus delight driblet a comment.

Subscribe to receive free email updates:

0 Response to "Difference betwixt save(), saveOrUpdate() in addition to persist() inwards Hibernate Session"

Posting Komentar