Hibernate: delete many-to-many association

Posted by Bar on Stack Overflow See other posts from Stack Overflow or by Bar
Published on 2010-05-06T18:49:40Z Indexed on 2010/05/06 19:18 UTC
Read the original article Hit count: 116

Filed under:
|
|

I have two tables with the many-to-many association.

— DB fragment:

loads
Id
Name

sessions
Id
Date

sessionsloads
LoadId
SessionId

— Hibernate mapping fragments:

/* loads.hbm.xml */
<set name="sessions" table="sessionsloads" inverse="true">
    <key column="LoadId" />
    <many-to-many column="SessionId" class="Session" />
</set>
…
/* sessions.hbm.xml */
<set name="loads" table="sessionsloads">
    <key column="SessionId" />
    <many-to-many column="LoadId" class="Load" />
</set>

In order to remove one entry from the association table sessionsloads, I execute this code:

Session session = sessionDao.getObject(sessionId);
Load load = loadDao.getObject(loadId);

load.getSessions().remove(session);
loadDao.saveObject(load);

But, after launching, this code change nothing.

What's the right way to remove an association?

© Stack Overflow or respective owner

Related posts about java

Related posts about hibernate