I have some problem with delation of associations, this is my code :
Classe ModuleVersion
@ManyToMany(mappedBy="listModuleVersions")
private List<SuiteVersion> suiteVersions = new ArrayList<SuiteVersion>();
Classe SuiteVersion
@ManyToMany()
private List<ModuleVersion> listModuleVersions = new ArrayList<ModuleVersion>();*
I would like to be able to delete automatically a suiteVersion or a moduleVersion with deletion of association. For example, if i delete a SuiteVersion with this code is delete automatically the association SuiteVersion to ModuleVersion. But if i delete a moduleVersion it doesn’t automatically delete the association of SuiteVersion -> ModuleVersion. It is possible to do this automatically with ModuleVersion ?
Update
This is the action perform by hibernate if i delete a module with the adding of CASCADETYPE.REMOVE …
Hibernate: delete from SUITE_VERSION_MODULE_VERSION where suiteVersions_ID_SUITE_VERSION=?
Hibernate: delete from SUITE_VERSION_MODULE_VERSION where suiteVersions_ID_SUITE_VERSION=?
Hibernate: delete from SUITE_VERSION_MODULE_VERSION where suiteVersions_ID_SUITE_VERSION=?
Hibernate: delete from SUITE_VERSION_MODULE_VERSION where suiteVersions_ID_SUITE_VERSION=?
Hibernate: delete from SUITE_VERSION_MODULE_VERSION where suiteVersions_ID_SUITE_VERSION=?
//Hibernate: delete from SUITE_VERSION where ID_SUITE_VERSION=?
//Hibernate: delete from SUITE_VERSION where ID_SUITE_VERSION=?
Hibernate: delete from MODULE_VERSION where ID_MODULE_VERSION=?
//Hibernate: delete from SUITE_VERSION where ID_SUITE_VERSION=?
//Hibernate: delete from SUITE_VERSION where ID_SUITE_VERSION=?
Hibernate: delete from MODULE_VERSION where ID_MODULE_VERSION=?
//Hibernate: delete from SUITE_VERSION where ID_SUITE_VERSION=?
Hibernate: delete from MODULE_VERSION where ID_MODULE_VERSION=?
Hibernate: delete from MODULE where ID_MODULE=?
I just want the part none comitted to be executed.
Thank you in advance for your help,
Update2
I tried to delete the module with this method implementation :
public static void removeModule(Module module) {
for(ModuleVersion moduleVersion : module.getListModuleVersion()){
for(SuiteVersion suiteVersion : moduleVersion.getSuiteVersions()){
sessionFactory = HibernateFactory.getSessionFactory();
session = sessionFactory.getCurrentSession();
transaction = session.beginTransaction();
suiteVersion.removeFromModuleVersions(moduleVersion);
transaction.commit();
}
}
sessionFactory = HibernateFactory.getSessionFactory();
session = sessionFactory.getCurrentSession();
session.delete(module);
transaction.commit();
}
But i have this problem now :
Exception in thread "AWT-EventQueue-0" java.util.ConcurrentModificationException
Best regards,
Florent.
P.S: I’m french, sorry for my english.
I think I got it now: you want to break an association and to get the corresponding record removed from the join table. That’s not what I understood from the original question. Anyway…
To do so, well, you have to do what I wrote, to break the association, i.e. to remove the corresponding entities from their respective association collection (you have to update both sides of the link too since it’s a bidirectional association). Something like this:
But I suggest using helper methods:
And then your code becomes:
See also
Update: Your “new” problem is a pure Java related problem: you can’t modify a collection while iterating on it, at least not with your current approach. You need to use an
IteratorandIterator#remove().Related questions