difference hibernate save vs persist vs saveOrUpdate

Difference between save and saveOrUpdate in Hibernate

Main difference between save and saveOrUpdate method is that save() generates a new identifier and INSERT record into database while saveOrUpdate can either INSERT or UPDATE based upon existence of record.

Clearly saveOrUpdate is more flexible in terms of use but it involves an extra processing to find out whether record already exists in table or not.

In summary save() method saves records into database by INSERT SQL query, Generates a new identifier and return the Serializable identifier back. On the other hand saveOrUpdate() method either INSERT or UPDATE based upon existence of object in database. If persistence object already exists in database then UPDATE SQL will execute and if there is no corresponding object in database than INSERT will run.



Difference between save and persist method in Hibernate

In last section we saw What are difference between save and saveOrUpdate and now we will see Difference on save vs persist method.
1)First difference between save and persist is there return type. Similar to save method persist also INSERT records into database but return type of persist is void while return type of save is Serializable object.
2) Another difference between persist and save is that both methods make a transient instance persistent. However, persist() method doesn’t guarantee that the identifier value will be assigned to the persistent instance immediately, the assignment might happen at flush time.
3) One more thing which differentiate persist and save method in Hibernate is that is there behavior on outside of transaction boundaries. persist() method guarantees that it will not execute an INSERT statement if it is called outside of transaction boundaries. save() method does not guarantee the same, it returns an identifier, and if an INSERT has to be executed to get the identifier (e.g. “identity” generator), this INSERT happens immediately, no matter if you are inside or outside of a transaction.
4) Fourth difference between save and persist method in Hibernate is related to previous difference on save vs persist. Because of its above behavior of persist method outside transaction boundary, its useful in long-running conversations with an extended Session context. On the other hand save method is not good in a long-running conversation with an extended Session context.
These were some differences between save, saveOrUpdate and persist method of Hibernate. All three method are related to saving Object into database but there behavior are quite different. Knowledge of save, persist and saveOrUpdate not only helps to decide better use of Hibernate API but also help you to do well in Hibernate interviews.

From here


“detached entity passed to persist error” with JPA

I got the answer, I was using:


I used merge in place of persist:


Here .persist() only will insert the record.If we use .merge() it will check is there any record exist with the current ID,If it exist it will update otherwise it will insert new record. 


The error you described occurs because the object’s ID is set. Hibernate distinguishes between transient and detached objects and persist works only with transient objects. If persist() concludes the object is detached (which it will because the ID is set), it will return the “detached object passed to persist” error. You can find more details here and here.

jpa merge vs persist

Either way will add an entity to a PersistenceContext, the difference is in what you do with the entity afterwards.

Persist takes an entity instance, adds it to the context and makes that instance managed (ie future updates to the entity will be tracked)

Merge creates a new instance of your entity, copies the state from the supplied entity, and makes the new copy managed. The instance you pass in will not be managed (any changes you make will not be part of the transaction – unless you call merge again).

code example:


MyEntity e = new MyEntity();

// scenario 1
// tran starts
// tran ends, and the row for someField is updated in the database

// scenario 2
// tran starts
e = new MyEntity();
// tran ends but the row for someField is not updated in the database (you made the changes *after* merging

// scenario 3
// tran starts
e = new MyEntity();
MyEntity e2 = em.merge(e);
// tran ends and the row for someField is updated (the changes were made to e2, not e)



前面用到contact和employee,需要在jsp里面创建contact后加入到employee的列表,貌似直接persist contact后,立即用这个contact的话会id 为null,相当苦恼!不知道是oracle的原因还是因为是在客户端里调用的原因。

今天在为了完成作业的任务之一–3个entity bean,强行加了一个一对一的phoneNumber to contact,以为会很简单,其实坐起来还是用了两个多小时,也好,更清楚的理解了一下orm!

当表格中得到phoneNumber后,就要立即instantiate一个phoneNumber bean,然后persist,问题跟以前一样,如果直接用这个bean来创建新contact的话,id会为空,然后当然就nullpointer来了!

解决方法是改造一下persist方法,以为在dao里面的话,persist之后那个obj是会有id的,于是在dao里面见一个persist的wrapper方法,让它返回刚persist的obj,这样就有id了! //其实这个就是merge方法,不用增加了,今天才发现。。。(11月2日)