“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.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s