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