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



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
Сайт очень качественный. Вам награду бы за него или почетный орден.
Reply to this
This has helped me no end, thanks
Reply to this
this has really helped me thanks
Reply to this
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
I'm sorry, but I know absolutely nothing about EJB 2.1.
Reply to this
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
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