Can anyone tell me what's the advantage of persist() vs save() in Hibernate?
From this forum post
I did some mock testing to record the difference between
Sounds like both these methods behaves same when dealing with Transient Entity but differ when dealing with Detached Entity.
For the below example, take EmployeeVehicle as an Entity with PK as
Example 1 : Dealing with Transient Object
Note the result is same when you get an already persisted object and save it
Repeat the same using
Example 2 : Dealing with Detached Object
Result : You might be expecting the Vehicle with id : 36 obtained in previous session is updated with name as "Toyota" . But what happens is that a new entity is saved in the DB with new Id generated for and Name as "Toyota"
Using persist to persist detached entity
So, it is always better to use Persist() rather than Save() as save has to be carefully used when dealing with Transient object .
Important Note : In the above example , the pk of vehicle entity is a generated value , so when using save() to persist a detached entity , hibernate generates a new id to persist . However if this pk is not a generated value than it is result in a exception stating key violated.
This question has some good answers about different persistence methods in Hibernate. To answer your question directly, with save() the insert statement is executed immediately regardless of transaction state. It returns the inserted key so you can do something like this:
So use save() if you need an identifier assigned to the persistent instance immediately.
With persist(), the insert statement is executed in a transaction, not necessarily immediately. This is preferable in most cases.
Use persist() if you don't need the insert to happen out-of-sequence with the transaction and you don't need the inserted key returned.
Actually the difference between hibernate save() and persist() methods depends on generator class we are using.
I have done good research on the save() vs persist() including running it on my local machine several times. All the previous explanations are confusing and are not correct. I've compared the save() and persist() below after a thorough research.
All these are tried/tested on
Basic rule says that :
For Entities with generated identifier :
save() : It returns an entity's identifier immediately in addition to making the object persistent. So an insert query is fired immediately.
persist() : It returns the persistent object. It does not have any compulsion of returning the identifier immediately so it does not guarantee that insert will be fired immediately. It may fire an insert immediately but it is not guaranteed. In some cases, the query may be fired immediately while in others it may be fired at session flush time.
For Entities with assigned identifier :
save(): It returns an entity's identifier immediately. Since the identifier is already assigned to entity before calling save, so insert is not fired immediately. It is fired at session flush time.
persist() : same as save. It also fire insert at flush time.
Suppose we have an entity which uses a generated identifier as follows :
Now suppose we have the same entity defined as follows without the id field having generated annotation i.e. ID will be assigned manually.
for save() :
for persist() :
The above cases were true when the save or persist were called from within a transaction.
The other points of difference between save and persist are :