Sunday, November 24, 2019

Difference Betwixt Save(), Saveorupdate() Too Persist() Inwards Hibernate Session

Session interface inward Hibernate provides a yoke of methods to motion an object from novel or transient soil to persistent soil e.g. save(), saveOrUpdate() together with persist() are used to shop an object into the database, only at that spot are to a greater extent than or less pregnant differences betwixt them. The Session.save() method does an INSERT to shop the object into the database together with it too render the identifier generated past times the database. On the other hand, saveOrUpdate() tin survive used to reattach a detached object inward Hibernate Session i.e. it tin produce INSERT or UPDATE depending upon whether object exists inward database or not. The tertiary method, persist() is similar to Session.save() i.e. it is used to motion a transient object to the persistent soil past times storing it into the database only it doesn't render the database identifier. This was the basic departure betwixt save() vs saveOrUpdate() vs persist() method together with advise when to piece of occupation which method. It is too 1 of the oft asked Hibernate interview questions on Java Web developer labor interviews. Knowing this especial aid you lot to produce good on your interview. Btw, if you lot choose non read already hence you lot must read Java Persistence alongside Hibernate, best mass to larn these kinds of subtle details nearly Hibernate.



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

As I said, all iii methods save(), persist() together with 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 alongside a Hibernate Session inward past times only currently it's non associated into active persistence context. It is too 1 of the most popular Hibernate interview questions. Let's run into yoke of to a greater extent than differences betwixt save(), persist() together with saveOrUpdate() methods of Session interface.




1) The principal departure between save() together with saveOrUpdate() method is that save() method performs an INSERT performance to shop the object into the database, only INSERT volition neglect if the primary key is already persistent i.e. object already exists inward the database. This is why, you lot should alone telephone band save() alongside an absolutely novel object which doesn't choose whatsoever database identifier. Calling save() alongside the detached object volition fail. This is reverse of saveOrUpdate() method, which tin produce either INSERT or UPDATE SQL query depending upon whether an object exists inward the database or not. The saveOrUpdate() method kickoff executes a SELECT query to decide if it needs to produce an INSERT or UPDATE operation.


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

Here is an object's lifecycle together with soil transition diagram inward Hibernate which shows that save() together with saveOrUpdate() tin motion an object from transient to persistent state.

Serializable object only 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, only the next trouble volition throw a compile fourth dimension fault because persist()'s render type is void.

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


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

Further Learning
Introduction To Hibernate
Spring together with Hibernate for Beginners
Hibernate Interview Questions Preparation Course

Thanks for reading this article, if you lot similar this article together with interview enquiry hence delight percentage alongside your friends together with colleagues. If you lot choose whatsoever enquiry or feedback hence delight driblet a comment.

No comments:

Post a Comment