• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Understanding merge(), and "detached entity passed to persist" Exception

 
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hibernate 3.3.1.GA (JPA 1.0) + EJB 3.0

Hi everyone,

I know that invoking merge() on a detached object should be just fine. Even if you explicitly invoke detach() operation to detach an object after the merge() operation and modify it is just fine. However it doesn't work on related entities if you explicitly invoke detach() operation on them and modify. And that's what I want to understand why. It throws "org.hibernate.PersistentObjectException: detached entity passed to persist"

Here's an example:



Could anyone enlighten me on this? AFAIK, merge should work on detached objects but seeing the result, it seems to me that it's not the merge() operation that is being cascaded on the related entities but the persist() operation.

Thanks!
 
Ranch Hand
Posts: 247
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I just now got some clarifications on "Detached" state of Object concept...

As you said,



How the detach() method can trigger this exception???...

And this exception usually occurs when you "persist()" a "Transient" object (newly instantiated) with having "Primary Key" field set with a value and Entity Manager assumes that this object is in Detached state...

Moreover,

In



Ensure that there is an associating member variable "a" in the class B...

 
Frankie Fuentes
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oops sorry, it's supposed to be mappedBy="parent" and not mappedBy="a".

The line this.getEm().detach( b ) doesn't throw the exception immediately. The exception is thrown after the transaction ends when the method has ended. Removing that line will make it work just fine.

What I don't understand is why it throws an exception when it is clearly legal to invoke merge() operation on a detached entity. The update() function demonstrates that it is legal to invoke merge() operation on detached or explicitly detached entity. update2() completely contradicts this and that's what I want to understand why.
 
Frankie Fuentes
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Anyone?
 
Ranch Hand
Posts: 43
Android Hibernate jQuery
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
when you detach a parent object all its child objects are also detached, so you are trying to detach an already detached entity..
 
Frankie Fuentes
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Anantha Sharma wrote:when you detach a parent object all its child objects are also detached, so you are trying to detach an already detached entity..



Looking at update2( A a ), it is clear that it doesn't detach the parent. Only the children get detached.
 
Frankie Fuentes
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Anyone?
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic