detached entity passed to persist

Problem: detached entity passed to persist

Possible Solution: This error can occur for a number of reasons. I suggest that if my solution doesn't help you, then you keep doing google searches. There's a lot of different forum posts about this one, and there's a lot of different problems that can cause the error message.

I was getting this error (apparently) because I was bi-jecting a Seam entity though multiple session beans. When I changed this to bi-jecting just the id for the entity and then doing an entity manager lookup using the id (in each of the session beans), the problem went away. I worked on this for a long, long time, and when I found a solution that worked, I was happy to just stick with it. However, I don't believe that this is the absolute correct solution, because it means doing a database lookup on the entity each time a new page is displayed.

Anyhow, right or not, here's the details. I had a Seam entity called coupon, defined in Coupon.java. My first screen is a table to look up the coupon to work with. Previously, I had this screen's session bean setting an out-jected conversation context variable called selectedCouponObj. Then, the next screens would inject selectedCouponObj and work with it. Note that this actually worked as far as getting and setting the values inside the object. BUT, as soon as I did an entityManager.persist(selectedCoupon), I'd get the detached entity error.

So, the fix was: instead of out-jecting and in-jecting the Coupon itself, I out-jected and in-jected the Coupon id: selectedCouponId. Then, I' set the selectedCouponObj in each session bean by calling selectedCouponObj = entityManager.find(Coupon.class, selectedCouponId)

Like I said, this does not seem like the ideal solution, but it worked for me.

 del.icio.us  Digg 

 
Trackbacks
  • No trackbacks exist for this entry.
Comments

  • 9/1/2009 6:06 AM pickster wrote:
    Thanks for this solution! Can you imagine my joy when I finally found it here after numerous searches at rapidshare and http://rapid4me.com search engine? It's a real treasure for me now.
    Reply to this
  • 9/23/2009 5:52 AM Pavlosius wrote:
    Сайт очень качественный. Вам награду бы за него или почетный орден.
    Reply to this
  • 11/13/2009 4:11 AM contact Skype wrote:
    This has helped me no end, thanks
    Reply to this
  • 11/23/2009 6:26 AM Immigration wrote:
    this has really helped me thanks
    Reply to this
  • 3/22/2010 5:46 AM Black Jack Strategie wrote:
    Thanks for the confirmation. Can I use injection technique with EJB 2.1 and/or in web only project? I am on EJB 2.1 and I tried to use injection technique but somehow entity manager is not getting injected by container and I get Null pointer exception.Could you please throw some light on this and how I would be able to use transaction in EJB 2.1?
    Reply to this
    1. 3/22/2010 8:05 AM Jeremy wrote:
      I'm sorry, but I know absolutely nothing about EJB 2.1.

      Reply to this
  • 6/10/2010 3:43 AM Nishu Gupta wrote:
    the user clicks on the "add button" to add the customer, within the business logic, the persist method of the EntityManager throws a PersistenceException. In fact, with Hibernate, this exception is thrown in a lazy way when the transaction tries to commit.
    Reply to this
  • 6/10/2010 4:20 AM nice home decore wrote:
    The contract for persist (see section 3.2.1 of the JPA 1.0 spec) explicitly states that an EntityExistsException is thrown by the persist method when the object passed in is a detached entity. Or any other PersistenceException when the persistence context is flushed or the transaction is committed.
    Reply to this
Leave a comment

Submitted comments will be subject to moderation before being displayed.

 Enter the above security code (required)

 Name

 Email (will not be published)

 Website

Your comment is 0 characters limited to 3000 characters.