Hello!
I have a problem with deleting an 1:n association.
I have two objects which have a bidirectional 1:n association. Now I want to delete this association without deleting the objects. However this is not working. I want to delete the association between the dashboard and one medical document object.
These are my two xml mapping files:
1. Dashboard mapping:
Code:
?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
<hibernate-mapping>
<class name="at.ac.uibk.dbis.dmis.server.application.findings.Dashboard" table="Dashboard">
<id name="PID" column="PID" >
<generator class="increment"/>
</id>
<set name="medicalDocuments" inverse="true" table="dashboard_medicalDocuments">
<key column="dashboard_id"></key>
<many-to-many class="at.ac.uibk.dbis.dmis.server.application.findings.MedicalDocument" column="medicalDocument_id" unique="true"></many-to-many>
</set>
</class>
</hibernate-mapping>
2. MedicalDocuments mapping:
Code:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
<hibernate-mapping package="at.ac.uibk.dbis.dmis.server.application.findings">
<class name="MedicalDocument" table="MedicalDocument" lazy="false">
<id name="PID" column="PID" >
<generator class="increment"/>
</id>
<property name="requestDate" column="RequestDate" />
<property name="receiptDate" column="ReceiptDate" />
<property name="docID" column="DocID" />
<property name="labID" column="LabID" />
<property name="loincName" column="LoincName" />
<property name="loincCode" column="LoincCode" />
<property name="title" column="Title" />
<property name="xmlFile" column="XMLFile" />
<property name="avaiable" column="Avaiable" />
<join table="patient_medicalDocuments">
<key column="medicalDocument_id"></key>
<many-to-one name="patient" class="at.ac.uibk.dbis.dmis.server.application.patient.Patient">
<column name="patient_id" not-null="true"></column>
</many-to-one>
</join>
<join table="dmissession_medicaldocuments">
<key column="medicalDocument_id"></key>
<many-to-one name="session" class="at.ac.uibk.dbis.dmis.server.application.session.DmisSession">
<column name="dmissession_id" not-null="true"></column>
</many-to-one>
</join>
<join table="dashboard_medicalDocuments">
<key column="medicalDocument_id"></key>
<many-to-one name="dashboard" class="at.ac.uibk.dbis.dmis.server.application.findings.Dashboard" cascade="all">
<column name="dashboard_id" not-null="true"></column>
</many-to-one>
</join>
</class>
</hibernate-mapping>
This is my Dashboard method:
Code:
public void removeMedicalDocument(MedicalDocument doc){
log.debug("Loading the medical document from the database...");
log.debug("Current number of medical documents on this dashboard:"+this.getMedicalDocuments().size());
List<MedicalDocument> docs = new ArrayList<MedicalDocument>(this.getMedicalDocuments());
for(int i = 0; i< docs.size(); i++){
if(docs.get(i).getPID() == doc.getPID()){
this.getMedicalDocuments().remove(docs.get(i));
}
}
log.debug("Updating now the database...");
this.updateDashboard();
log.debug("Update finished...");
log.debug("Current number of medical documents on this dashboard:"+this.getMedicalDocuments().size());
}
public void updateDashboard(){
DashboardHibernate.updateDashboard(this);
}
And this is my hibernate file where a make the update:
Code:
public static DmisSession updateDmisSession(DmisSession dmisSession){
try {
Session session = SessionFactoryUtil.getInstance().getCurrentSession();
Transaction tx = session.beginTransaction();
session.update(dmisSession);
tx.commit();
return dmisSession;
}
catch(RuntimeException e){
try {
Session session = SessionFactoryUtil.getInstance().getCurrentSession();
if(session.getTransaction().isActive())
session.getTransaction().rollback();
}
catch (HibernateException e1){
log.error("Error rolling back transaction");
}
throw e;
}
}
What am I doing worng thet the association is not deleted?
Thanks,
Florian