-->
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: Hibernate: delete by HQL
PostPosted: Wed Oct 29, 2008 6:55 am 
Newbie

Joined: Wed May 24, 2006 5:47 pm
Posts: 14
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.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.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


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