Hi.
I have this environmet:
Hibernate 3.2.6
JVM 1.4.2_12
JBoss 4.0.5
Liferay 4.3.1
I have a one-to-many relation between an entity (called Ufficio) that is the parent and another entity (called Recapito) that is the child.
I have these hbm:
Ufficio.hbm.xml
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.sua.intranet.groupware.rubrica.dao.model.Ufficio" table="SUA_RUBRICA_UFFICIO" >
<id name="idUfficio" type="int">
<column name="ID_UFFICIO" />
<generator class="native" />
</id>
<property name="descrizione" type="string">
<column name="DESCRIZIONE_UFFICIO" length="250" not-null="true">
<comment></comment>
</column>
</property>
<property name="ordine" type="int">
<column name="ORDINE" length="20" not-null="false" default="0">
<comment></comment>
</column>
</property>
<set name="recapiti" inverse="true" [b][color=red]cascade="all-delete-orphan"[/color][/b]>
<key column="ID_UFFICIO"></key>
<one-to-many class="it.eng.sua.intranet.groupware.rubrica.dao.model.Recapito"/>
</set>
<set name="indirizzi" inverse="true" cascade="all-delete-orphan">
<key column="ID_UFFICIO"></key>
<one-to-many class="it.eng.sua.intranet.groupware.rubrica.dao.model.Indirizzo"/>
</set>
<set name="persone" table="UFFICIO_PERSONA">
<key column="ID_UFFICIO"></key>
<many-to-many column="ID_PERSONA" class="it.eng.sua.intranet.groupware.rubrica.dao.model.Persona"></many-to-many>
</set>
<!-- Relazione molti a uno con servizio; una servizo puo' avere diversi uffici-->
<many-to-one name="servizio" column="ID_SERVIZIO" not-null="false"></many-to-one>
<!-- Relazione molti a uno con persona; una struttura puo' avere un solo responsabile-->
<many-to-one name="responsabile" column="ID_RESPONSABILE" class="it.eng.sua.intranet.groupware.rubrica.dao.model.Persona" not-null="true"></many-to-one>
</class>
</hibernate-mapping>
Recapito.hbm.xmlCode:
<?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.sua.intranet.groupware.rubrica.dao.model.Recapito" table="SUA_RUBRICA_RECAPITO" >
<id name="idRecapito" type="int">
<column name="ID_RECAPITO" />
<generator class="native" />
</id>
<property name="telefono" type="string">
<column name="TELEFONO" length="50" not-null="false">
<comment></comment>
</column>
</property>
<property name="fax" type="string">
<column name="FAX" length="50" not-null="false">
<comment></comment>
</column>
</property>
<property name="email" type="string">
<column name="E_MAIL" length="50" not-null="false">
<comment></comment>
</column>
</property>
<property name="ordine" type="int">
<column name="ORDINE" length="20" not-null="false" default="0">
<comment></comment>
</column>
</property>
<!-- Relazione molti a uno con ufficio -->
<many-to-one name="ufficio" column="ID_UFFICIO" not-null="false"/>
<!-- Relazione molti a uno con persona -->
<many-to-one name="persona" column="ID_PERSONA" not-null="false"/>
<!-- Relazione molti a uno con servizio -->
<many-to-one name="servizio" column="ID_SERVIZIO" not-null="false"/>
<!-- Relazione molti a uno con struttura -->
<many-to-one name="struttura" column="ID_STRUTTURA" not-null="false"/>
</class>
</hibernate-mapping>
Now in my portle I have a form where the user chooses some ids of the entities Ufficio; i save these id in an arrya list and than i try to delete this entities by using this code:
Code:
List idss = Collections.synchronizedList(new ArrayList());
for (int i = 0; i < ids.size(); i++) {
idss.add(new Integer(((String) (ids.get(i))).trim()));
}
String hql = "delete from Ufficio where idUfficio in (:ids)";
Query query = session.createQuery(hql).setParameterList("ids", idss);
query.executeUpdate();
I'm sure that some entities Ufficio i want to delete have some entities Recapito associated but for me it's reasonable to delete Ufficio and its Recapito (after i adviced the user). When i try to delete the Ufficio i got an error saying to me that the update can't be done since a foreign key is violated...
Now if we see my hbm i inserted the cascade option
all-delete-orphan. Does it mean that when i try to delete Ufficio alle the Ufficio's orphan are deleted?
Where am i wrong?
Am I missing anything?
Thanx to all.
Angelo