-->
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.  [ 2 posts ] 
Author Message
 Post subject: Howto only delete un-referenced entities with cascade?
PostPosted: Tue Nov 04, 2008 6:58 pm 
Beginner
Beginner

Joined: Tue Jul 03, 2007 8:47 am
Posts: 46
Hello,

I have two Classes which have a ManytoMany relationship with each other:
Code:
public class Peer implements Serializable{
........
    @ManyToMany(cascade = { CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REMOVE })
    private Set<SharedFile> files = new HashSet<SharedFile>();
}

public class SharedFile implements Serializable {
.............   
    @ManyToMany(cascade = { CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REMOVE })
    private Set<Peer> peers = new HashSet<Peer>();


So a Peer can have many shared files, and a shared file can have many peers.
However when I delete a single peer (using session.delete(peer)), ALL referenced SharedFiles are also deleted, even if they are still referenced by other Peers. Deleting the SharedFile cascades to their peers and in the end I have an empty database.

Is there any way to only cascade-delete SharedFile, if no other peer does reference it?
If I remove the CascadeType.Remove from SharedFile (to not delete other Peers which share the same file), I get an exception:
Code:
SCHWERWIEGEND: Integrity constraint violation FK286ACA391E1BF993 table: PEER_FILE in statement [delete from FILE where file_id=?]
04.11.2008 22:56:12 org.hibernate.event.def.AbstractFlushingEventListener performExecutions
SCHWERWIEGEND: Could not synchronize database state with session


Thank you in advance, Clemens


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 05, 2008 1:23 am 
Expert
Expert

Joined: Mon Nov 26, 2007 2:29 pm
Posts: 443
Which side is the inverse?

_________________
Gonzalo Díaz


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 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.