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

Leave a comment

 Enter the above security code (required)

 Name

 Email (will not be published)

 Website

Your comment is 0 characters limited to 3000 characters.