Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Frankie Fuentes
Ranch Hand
Posts: 41
  • 0
  • Mark post as helpful
  • send pies
  • 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!
 
Ram Narayan.M
Ranch Hand
Posts: 247
Chrome Eclipse IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anyone?
 
Anantha Sharma
Ranch Hand
Posts: 43
Android Hibernate jQuery
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anyone?
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic