-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 posts ] 
Author Message
 Post subject: Delete detached objects problem
PostPosted: Mon Mar 01, 2010 5:12 am 
Beginner
Beginner

Joined: Thu Nov 26, 2009 8:20 am
Posts: 23
Hello,
I am using hibernate with JPA and I am working with detached Objects.

What I want is to load the data from the DB, modify it and then update the DB.
Most is working fine so far, but there is still one Problem left: Delete Objects!

I am using merge() to update the DB at least. I can change the data but I can't delete objects and merge.

The following Exception is thrown:

org.hibernate.ObjectDeletedException: deleted instance passed to merge: ....

Are there any specials deleting a detached object?

I would appreciate any help, thanks in advance.

Best regards
Alex


Top
 Profile  
 
 Post subject: Re: Delete detached objects problem
PostPosted: Mon Mar 01, 2010 5:49 am 
Expert
Expert

Joined: Tue Jun 16, 2009 3:36 am
Posts: 990
Quote:
org.hibernate.ObjectDeletedException: deleted instance passed to merge: ....


This message means that the regarding object is already marked for being deleted.
It makes no sense trying to delete it a second time, so this is why you get this exception.


Top
 Profile  
 
 Post subject: Re: Delete detached objects problem
PostPosted: Mon Mar 01, 2010 7:43 am 
Beginner
Beginner

Joined: Thu Nov 26, 2009 8:20 am
Posts: 23
First of all: thanks for your answer.

It seems that there is still another problem, because I definetly do not delete the object twice!
I recognized that I have another problem, perhaps something with the Cascading-Types is wrong

My model is simple: Packages and Dependencies as connections between 2 Packages:

Code:
public class ModelPackage {
@OneToMany(
         fetch = FetchType.EAGER,
         cascade = CascadeType.MERGE,
         mappedBy = "source")
   private Set<Dependency> sourceDependencies = new HashSet<Dependency>();
   
   @OneToMany(
         fetch = FetchType.EAGER,
         cascade = CascadeType.MERGE,
         mappedBy = "target")
   private Set<Dependency> targetDependencies = new HashSet<Dependency>();
...

}


and Dependency:
Code:
public class Dependency extends DiagramElement {   
   
   @ManyToOne (fetch = FetchType.EAGER, cascade=CascadeType.MERGE)
   private ModelPackage source;

   @ManyToOne (fetch = FetchType.EAGER, cascade=CascadeType.MERGE)
   private ModelPackage target;
...
}


I can create 2 Packages and a Dependency between those 2 Packages and call merge() without any problems.
But if I do the following order:
create 2 Packages and a Dependency between those 2 Packages
merge();
create a third Package
merge()
create a fourth Package and a Dependency between Package 3 and Package 4
merge() -> Exception:

org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing: xyz.Dependency

I am a little bit confused because I do not know why the first way works and the second doesn't...

Thanks in advance
Best regards
Alex


Top
 Profile  
 
 Post subject: Re: Delete detached objects problem
PostPosted: Mon Mar 01, 2010 8:43 am 
Expert
Expert

Joined: Tue Jun 16, 2009 3:36 am
Posts: 990
Quote:
org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing: xyz.Dependency


Since you don't use Cascade.PERSIST you must persist (save) explicitly all new object's when you associate them.

Code:
ModelPackage modelPackage = new ModelPackage();
session.save(modelPackage);

Dependency dep = new Dependency();
session.save(dep);      /// probably you are missing this line !!!!!!!!


modelPackage.addTargetDependency(dep);
dep.setTarget(modelPackage);


Top
 Profile  
 
 Post subject: Re: Delete detached objects problem
PostPosted: Mon Mar 01, 2010 9:19 am 
Beginner
Beginner

Joined: Thu Nov 26, 2009 8:20 am
Posts: 23
Hello and thanks again for your answer.

It doesn't matter if I use Cascade.ALL/PERSIST/MERGE, it does'nt work.

The problem is, that I want to use the detached object concept (long unit of work).
What I want is to load the data from the DB, modify it and then update the DB without any save()/persist()/delete() operations on my EntityMangager, just by calling merge(model) at the end of a session.

On "simple" objects it works fine, I can create Packages and save them into db via merge.
But it doesn't work with connections, so am I doing something wrong or is the way I try to do it wrong at all?

BTW: The first connection works still fine, too. The connection was saved in the database and no exception was thrown.
So for me it seems there is another problem...

Thanks in advance,
best regards
Alex


Top
 Profile  
 
 Post subject: Re: Delete detached objects problem
PostPosted: Tue Mar 02, 2010 6:16 am 
Beginner
Beginner

Joined: Thu Nov 26, 2009 8:20 am
Posts: 23
Hello again,
I can solve most of my problems by telling hibernate to persist new objects and delete removed objects and merging it at the end.
One problem is still left: Undo/Redo.

What I want is, that the user can for example delete a Package and undo/redo this operation.

How can I implement that in hibernate?

//delete it
Code:
entityManager.delete(package);
....
//later undo (by user)
entityManager.persist(package);


That does not work because I try to persist a deleted entity. Any ideas how to solve it?

I would appreciate any help!

Best regards
Alex


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.