• Menu
  • Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

JavaBeat

Java Tutorial Blog

  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)
  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)

Hibernate / JPA – Detached entity passed to persist exception

April 21, 2015 //  by Krishna Srinivasan//  Leave a Comment

You are here because you have just thrown out by your application with this annoying exception : javax.persistence.PersistenceException: org.hibernate.PersistentObjectException: detached entity passed to persist. There are multiple reasons that could cause this exception. But, basic idea behind this exception is that the object which you are trying to persist is not in the persistant state. Note that this exception will be thrown even if you dont have the transaction enabled in your application.

also read:

  • Spring + Primefaces + Hibarnate
  • Introduction to Spring OXM

One of the complicated issue while working with Hibernate / JPA projects are that differentiating the state of persistent objects. If you no longer required an object, programatically one can detach an entity from the transaction and it will be no longer in the persistence state.

In many cases, the object would have been considered as detached even though you are not intentionally done that.

  1. Check if you are trying to persist an entity which has the same id as another entity, and which is already present in the PersistenceContext in your application.
  2. Do not set an ID before you save or persist it. Hibernate will look at the Entity you’ve passed and it assumes that because it has its Primary Key populated that it is already in the database.
  3. If you are working with Spring MVC application, check that whether you have added <tx:annotation-driven/> in your configuration file. If you are missing this line, transactions would not have been started in your application. This will ultimately throw exception javax.persistence.PersistenceException: org.hibernate.PersistentObjectException: detached entity passed to persist.

If you are still not able to resolve this issue, please write it in the comments section.

Category: HibernateTag: Hibernate Exceptions

About Krishna Srinivasan

He is Founder and Chief Editor of JavaBeat. He has more than 8+ years of experience on developing Web applications. He writes about Spring, DOJO, JSF, Hibernate and many other emerging technologies in this blog.

Previous Post: « Introduction to Hibernate Envers
Next Post: Node.js + MongoDB – Performing CRUD Operations »

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

Follow Us

  • Facebook
  • Pinterest

FEATURED TUTORIALS

New Features in Spring Boot 1.4

Difference Between @RequestParam and @PathVariable in Spring MVC

What is new in Java 6.0 Collections API?

The Java 6.0 Compiler API

Introductiion to Jakarta Struts

What’s new in Struts 2.0? – Struts 2.0 Framework

JavaBeat

Copyright © by JavaBeat · All rights reserved
Privacy Policy | Contact