Difference between save(), saveOrUpdate() and persist() in Hibernate Session

Session interface in Hibernate provides a couple of methods to move an object from new or transient state to persistent state e.g. save(), saveOrUpdate() and persist() are used to store an object into the database, but there are some significant differences between them. The Session.save() method does an INSERT to store the object into the database and it also return the identifier generated by the database. On the other hand, saveOrUpdate() can be used to reattach a detached object in Hibernate Session i.e. it can do INSERT or UPDATE depending upon whether object exists in database or not. The third method, persist() is similar to Session.save() i.e. it is used to move a transient object to the persistent state by storing it into the database but it doesn't return the database identifier. This was the basic difference between save() vs saveOrUpdate() vs persist() method and suggest when to use which method. It is also one of the frequently asked Hibernate interview questions on Java Web developer job interviews. Knowing this detail help you to do well on your interview. Btw, if you have not read already then you must read Java Persistence with Hibernate, best book to learn these kinds of subtle details about Hibernate.



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

As I said, all three methods save(), persist() and saveOrUpdate belongs to Hibernate's Session interface. They all are used to bring a new object into persistence context or a detached object, which was associated with a Hibernate Session in past but currently it's not associated into active persistence context. It is also one of the most popular Hibernate interview questions. Let's see couple of more differences between save(), persist() and saveOrUpdate() methods of Session interface.



1) The main difference between save() and saveOrUpdate() method is that save() method performs an INSERT operation to store the object into the database, but INSERT will fail if the primary key is already persistent i.e. object already exists in the database. This is why, you should only call save() with an absolutely new object which doesn't have any database identifier. Calling save() with the detached object will fail. This is opposite of saveOrUpdate() method, which can do either INSERT or UPDATE SQL query depending upon whether an object exists in the database or not. The saveOrUpdate() method first executes a SELECT query to determine if it needs to do an INSERT or UPDATE operation.


2) Another key difference between save() and saveOrUpdate() method is that former is used to bring a transient object to persistent state but saveOurUpdate() can bring both transient (new) and detached (existing) object into persistent state. It is often used to re-attach a detached object into Session.

Here is an object's lifecycle and state transition diagram in Hibernate which shows that save() and saveOrUpdate() can move an object from transient to persistent state.









3) Coming to persist() method, the difference between save() and persist() method is that former returns the generated database identifier, a Serializable object but persist() method doesn't return anything. It's return type is void.

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

will print the generated primary key, but the following line will throw a compile time error because persist()'s return type is void.

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


That's all about difference between save(), saveOrUpdate() and persist() method of Hibernate Session interface. Now you know the basic difference between them and also learned when to use the save(), saveOrUpdate() and persist() method. Use the save() method to store new object into database and when you need the generated database identifier, otherwise use persist() method. You can use saveOrUpdate() to reattach a detached object into Hibernate Session.

Further Reading
Java Persistence with Hibernate By Gavin King (see here)
Best Hibernate Books for Java Developers (see here)


No comments:

Post a Comment