I have a piece of code that 'updates' an entry in the database by first cascade deleting it, and then replacing it with a new object, because I had a lot of problems trying to update it normally. This hack worked fine, until I started using some ManyToMany relations. This is the relation that is giving problems:
@ManyToMany(cascade = CascadeType.ALL)
private Set<RefProg> refProgSet = new HashSet<RefProg>();
It resides in a domain object called PDBEntry. The RefProg 开发者_JS百科domain object has the following piece of code:
@ManyToMany(mappedBy = "refProgSet")
private Set<PDBEntry> pdbEntrySet = new HashSet<PDBEntry>();
Now as you might have guessed, this goes terribly wrong when you try deleting a PDBEntry. Let's say PDBEntry "ENT1" has a RefProg "REFPROG1", and PDBEntry "ENT2" has the same RefProg. Now when I try to cascade delete "ENT1", it also tries to delete "REFPROG1" which should be impossible, since it is still linked to "ENT2". But if I don't set the RefProg to cascade delete, it won't delete the related rows in the jointable that Hibernate builds for me, giving me an error that it can't delete the PDBEntry. And since Hibernate is making the jointable it seems I have no access to it. Am I just too stubborn and should I have used an update instead of first trying to delete an entry before entering a new one, or is there some other way to make this work? Maybe is there some annotation to make the cascade get into the jointable and no further? Ideas?
Hav you tried to remove the PDBEntry from the pdbEntrySet and/or vice-versa before making the call to delete the PDBEntry?
精彩评论