views:

66

answers:

1

Hello,

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.

+2  A: 

Thank you for you answer. But if i had this. the suiteVersion is also deleted. I just want the row in the table of associations suiteVersion -> moduleVersion be deleted.

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:

session.getTransaction().begin();
SuiteVersion suite = ... // retrieved in some way
ModuleVersion module = ... // retrieved in some way
suite.getModuleVersions().remove(module);
module.getSuiteVersions().remove(suite);
session.getTransaction().commit(); // the "association" record should get removed

But I suggest using helper methods:

@Entity
public class SuiteVersion {
    ...
    @ManyToMany
    private List<ModuleVersion> moduleVersions = new ArrayList<ModuleVersion>();

    ...

    public void removeFromModuleVersions(ModuleVersion module) {
        this.moduleVersions.remove(module);
        module.getSuiteVersions().remove(this);
    }

    public void addToModuleVersions(ModuleVersion module) {
        this.moduleVersions.add(module);
        module.getSuiteVersions().add(this);
    }

}

And then your code becomes:

session.getTransaction().begin();
SuiteVersion suite = ... // retrieved in some way
ModuleVersion module = ... // retrieved in some way
suite.removeFromModuleVersions(module);
session.getTransaction().commit();

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 Iterator and Iterator#remove().

Related questions

Pascal Thivent
Thank you for you answer. But if i had this. the suiteVersion is also deleted. I just want the row in the table of associations suiteVersion -> moduleVersion be deleted.
Delildor
I update my post if you can take a look. Thank you
Delildor
@Delildor: I think I misunderstood your initial question, see my update.
Pascal Thivent
i try to test your method for deleting the module but i'm facing to another problem. (see update2). Thank you.
Delildor
@Delildor I covered this new problem in my answer but please avoid missing problems in one question next time (your new problem should have been posted as a new question, this is an unrelated issue). One question = one problem.
Pascal Thivent
Thank you for your answer. It resolve my problem. Next Time, i will post a new question like you notice.
Delildor