-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: Many-To-Many Relation bi-directional <delete error>
PostPosted: Fri Feb 26, 2010 10:13 am 
Newbie

Joined: Mon Mar 26, 2007 10:48 am
Posts: 13
Hi guys - i've a big problem.

I have 3 tables - personal - missions - personal_missions.

many-to-many - mission could have many personals and personals could be in a lot of missions....

Personal.hbm.xml
Code:
class name="com.dol.Personal" table="personal">
   <id name="personalId" column="personalId" type="java.lang.Integer"/>
   <bag name="missions" table="personal_mission" inverse="true">
      <key column="personalId"/>
      <many-to-many column="missionId" class="com.dol.Mission" />
   </bag>
</class>

Mission.hbm.xml
Code:
<class name="com.dol.Mission" table="mission">
   <id name="einsatzId" column="missionId" type="java.lang.Integer"/>
   <property name="desc" column="desc" type="java.lang.String" />
        <bag name="personal" table="personal_mission">
      <key column="missionId"/>
      <many-to-many column="personalId"  class="com.dol.Personal"/>
   </bag> 
</class>


Now with my entityManager - there are two methods - (addPersonalToMission and removePersonalFromMission).

Code:
public boolean addPersonalToMission(int personalId, int missionId){
               boolean ret = false;
          startOperation();
          Personal personal = (Personal) session.load(Personal.class, personalId);
          Mission mission = (Mission)session.load(Mission.class, missionId);
          if(!personal.getMissions().contains(mission))
          {
             mission.getPersonal().add(personal);
             ret = true;
          }
          session.save(mission);
          session.getTransaction().commit();
               return ret;
    }


In Both domainObjects (Personal.java and Mission.java) there are java.util.List which saves the parent.

The adding/removing works with this configurations - but when i change a mission (edit the description), then all assignments in the table personal_mission are deleted.... like cascading or so..

Does anyone know why this happen? it must be possible to edit a mission without consequences....

lg
triple

ps: Before this i tried another scenario:

i changed the configuration of Mission.hbm.xml and set inverse=true - so personal and mission has the inverse=true attribute.. so editing is possible - but adding a personal to mission or inversly isn't possible....

I know, one side must have the control .. but is there any possible solution?

i update the mission with following code:
Code:
public void updateMission(Mission newMission) throws Exception
   {
        try {
           closeOpenSession();
            startOperation();
            session.update(newMission);
            tx.commit();
        } catch (Exception e) {
         if (tx != null && tx.isActive())
            tx.rollback();
         throw e;
        }
    }


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
cron
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.