-->
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.  [ 2 posts ] 
Author Message
 Post subject: many-to-many: delete only one object and the row in the join
PostPosted: Thu Nov 15, 2007 7:52 am 
Newbie

Joined: Wed May 24, 2006 5:47 pm
Posts: 14
Hi all.

I have this scenario:

i have two entities: OrganoRub and NominativoRub; the relation between them is many to many...that is an OrganoRub can have from 0 to infinite NominativoRub and viceversa a NominativoRub can have from 0 to infinite OrganoRub.
Besides i need that when i create an OrganoRub and this OrganoRub has a non persisted NominativoRub associated this NominativoRub must be created.
Then if i delete an OrganoRub the associated Nominativo must not be deleted; only the row in the join table must be deleted; the same for NominativoRub..if a NominativoRub is deleted the eventual OrganoRub associated must not be deleted; only the the NominativoRub is deleted and the row in the join table.
Now i have used this mapping files:

NominativoRub

Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>
<class name="it.eng.intranet.bean.NominativoRub" table="NOMINATIVO_RUB">
   <id name="idNominativo" type="int" column="NOMINATIVO_ID" unsaved-value="0">
      <generator class="native"/>
   </id>
   <property name="cognome" column="COGNOME" type="string"/>
   <property name="matricola" column="MATRICOLA" type="int" length="10"/>
   <property name="indirizzoIp" column="INDIRIZZO_IP" type="string" length="15"/>
   <property name="nome" column="NOME" type="string"/>
   <property name="funzione" column="FUNZIONE" type="string"/>
   <property name="indirizzo" column="INDIRIZZO" type="string"/>
   
   <!--many-to-one name="sedeRub" column="SEDE_FK" not-null="false" class="it.eng.intranet.bean.SedeRub"/-->
   
   <!-- relazione *-* con la tabella ORGANO; un nominativo può avere diversi organi associati; esso potrebbe essere responsabile di diversi organo -->
   <set name="organi" inverse="true" lazy="false" table="NOMINATIVO_ORGANO">
        <key column="ID_NOMINATIVO"/>
        <many-to-many class="it.eng.intranet.bean.OrganoRub" column="ID_ORGANO"/>
    </set>
</class>
</hibernate-mapping>


OrganoRub

Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>
<class name="it.eng.intranet.bean.OrganoRub" table="ORGANO_RUB">
   <id name="idOrgano" type="int" column="ORGANO_ID">
      <generator class="native" />
   </id>
   <property name="codOrgano" column="CODICE_ORGANO" type="string" not-null="false"/>
   <property name="descrizione" column="DESCRIZIONE" type="string" length="10000" not-null="true"/>
   <!-- relazione *-1 con la tabella NOMINATIVO >
   <one-to-one name="nominativoRub" class="it.eng.bean.NominativoRub"/-->
   <!-- Relazione 0->* con Nominativo; un organo più avere 1 o N responsabili -->
   <set name="responsabili" cascade="save-update" table="NOMINATIVO_ORGANO">
      <key column="ID_ORGANO"/>
      <many-to-many class="it.eng.intranet.bean.NominativoRub" column="ID_NOMINATIVO"/>
   </set>      
</class>
</hibernate-mapping>


This is my environment:
    DB: MySql 5
    Hibernate version: 3.2.5
    Spring version: 2.0.6
    JVM: 1.4.2_12


Now I'm able in deleteing OrganoRub without deleting NominativoRub (only the row in the join table is deleted) but if i delete the NominativoRub if it has some OrganoRub associated i can't delete it....i need to delete it and the row in the join table....is there any solution?

Thanks to all.
Regards,
Angelo


Top
 Profile  
 
 Post subject: Deleting through associations
PostPosted: Thu Nov 15, 2007 4:01 pm 
Newbie

Joined: Thu Nov 15, 2007 1:36 pm
Posts: 12
Location: Ahsburn, VA
Quote:
...but if i delete the NominativoRub if it has some OrganoRub associated i can't delete it....i need to delete it and the row in the join table....


I believe it's your responsibility to programatically delete both side of an association; that way Hibernate knows nothing is referencing the entity.


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

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:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.