-->
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.  [ 6 posts ] 
Author Message
 Post subject: Delete child in an existing collection
PostPosted: Tue May 11, 2004 6:49 am 
Beginner
Beginner

Joined: Mon Sep 15, 2003 9:10 am
Posts: 43
I have an object with a collection.

i create the parent object and 2 childs, i save : ok.

i delete 2 object from the set and create a new one, save it and add to the set.

i save the collection : i have in database 3 childs (2 with null as parent id and one with the correct parent id). instead of 1.

I tried all the cascade options nothing, an idea ?

Thanks for your help,

Fabien


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 11, 2004 7:13 am 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
Cascade "all-delete-orphans" or delete the child manually.

_________________
JAVA PERSISTENCE WITH HIBERNATE
http://jpwh.org
Get the book, training, and consulting for your Hibernate team.


Top
 Profile  
 
 Post subject: Same probem
PostPosted: Tue May 11, 2004 8:09 am 
Beginner
Beginner

Joined: Mon Sep 15, 2003 9:10 am
Posts: 43
I tried "all-delete-orphan" (without s) u say and i have the same problem....


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 11, 2004 8:11 am 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
Please read the red text before posting.

_________________
JAVA PERSISTENCE WITH HIBERNATE
http://jpwh.org
Get the book, training, and consulting for your Hibernate team.


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 11, 2004 8:35 am 
Beginner
Beginner

Joined: Mon Sep 15, 2003 9:10 am
Posts: 43
Sorry :-(

Hibernate version : hibernate-2.1.2

Mapping document :

"The parent" :

Code:
<?xml version="1.0"?>

<!DOCTYPE hibernate-mapping PUBLIC
    "-//Hibernate/Hibernate Mapping DTD 2.0//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">

<hibernate-mapping>
    <class
        name="framework.entreprise.ModeleRecherche"
        table="modelesrecherche"
        dynamic-update="false"
        dynamic-insert="false"
    >

        <id
            name="id"
            column="id"
            type="long"
            unsaved-value="-1"
        >
            <generator class="native">
            </generator>
        </id>

        <property
            name="libelle"
            type="java.lang.String"
            update="true"
            insert="true"
            column="libelle"
        />

        <many-to-one
            name="utilisateur"
            class="framework.entreprise.Utilisateur"
            cascade="none"
            outer-join="auto"
            update="true"
            insert="true"
            column="id_utilisateur"
        />

        <set
            name="zones"
            lazy="true"
            inverse="false"
            cascade="save-update"
            sort="unsorted"
        >

              <key
                  column="id_recherche"
              />

              <one-to-many
                  class="framework.entreprise.ZoneModeleRecherche"
              />
        </set>

        <!--
            To add non XDoclet property mappings, create a file named
                hibernate-properties-ModeleRecherche.xml
            containing the additional properties and place it in your merge dir.
        -->

    </class>

</hibernate-mapping>


"The child":

Code:
<?xml version="1.0"?>

<!DOCTYPE hibernate-mapping PUBLIC
    "-//Hibernate/Hibernate Mapping DTD 2.0//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">

<hibernate-mapping>
    <class
        name="framework.entreprise.ModeleRecherche"
        table="modelesrecherche"
        dynamic-update="false"
        dynamic-insert="false"
    >

        <id
            name="id"
            column="id"
            type="long"
            unsaved-value="-1"
        >
            <generator class="native">
            </generator>
        </id>

        <property
            name="libelle"
            type="java.lang.String"
            update="true"
            insert="true"
            column="libelle"
        />

        <many-to-one
            name="utilisateur"
            class="framework.entreprise.Utilisateur"
            cascade="none"
            outer-join="auto"
            update="true"
            insert="true"
            column="id_utilisateur"
        />

        <set
            name="zones"
            lazy="true"
            inverse="false"
            cascade="all-delete-orphan"
            sort="unsorted"
        >

              <key
                  column="id_recherche"
              />

              <one-to-many
                  class="framework.entreprise.ZoneModeleRecherche"
              />
        </set>

        <!--
            To add non XDoclet property mappings, create a file named
                hibernate-properties-ModeleRecherche.xml
            containing the additional properties and place it in your merge dir.
        -->

    </class>

</hibernate-mapping>



Sample code :

[code]

tx= s.beginTransaction();

ModeleRecherche m = new ModeleRecherche();
ZoneModeleRecherche z1 = new ZoneModeleRecherche();
ZoneModeleRecherche z2 = new ZoneModeleRecherche();

m.setLibelle("Mon modele de test");
m.setZones(new HashSet());

s.save(z1);
m.getZones().add(z1);
s.save(z2);
m.getZones().add(z2);

s.save(m);
s.flush();

m.getZones().removeAll(m.getZones());
ZoneModeleRecherche z3=new ZoneModeleRecherche();
s.save(z3);
m.getZones().add(z3);

s.save(m);
s.flush();

tx.commit();
[code]


Database : MySQL - 4.0.12


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 11, 2004 8:42 am 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
You have to set both sides of the association in your code.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 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.