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;
}
}