-->
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.  [ 4 posts ] 
Author Message
 Post subject: Removing object from a collection doesn't seem to work
PostPosted: Fri Apr 30, 2004 8:20 am 
Beginner
Beginner

Joined: Tue Sep 30, 2003 4:16 pm
Posts: 33
I am using Hibernete 2.1, with HSQLDB 1.7.1.

Here is the code that is causing me a problem. It is intended simply to remove a certain child from a collection. For testing purposes this particular snippet is locating the first child and deleting it. At the end of this, the child is still there. If I follow it in the debugger, the first thing that happens inside the 'remove' method, is some kind of a Set.Add method, which I don't understand and at any rate, after the remove method returns if I look at the collection in the debugger, the child has not been renoved... Can you see what's wrong?

Thanks!

Code:
// ensure that 'group' is updated
session = sessions.openSession()
transaction = session.beginTransaction();
session.update(group);

// get first channel in collection
Collection channels = group.getChannelGroup().getChannels();
Iterator iter = channels.iterator();
channel = (Channel) iter.next();

transaction.commit();
session.flush();
session.close();
session = null;
transaction = null;

// Now actually do the remove
session = sessions.openSession()
transaction = session.beginTransaction();
group.remove(channel);
transaction.commit();
session.flush();
session.close();
session = null;
transaction = null;

When I run through this code, and check the number of children that group has, it doesn't appear to be actually removing the child.

Here is the mapping for 'group':

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="de.nava.informa.impl.hibernate.ChannelGroup"
        table="CHANNEL_GROUPS"
        dynamic-update="false"
        dynamic-insert="false"
    >

        <id
            name="intId"
            column="CHANNEL_GROUP_ID"
            type="integer"
        >
            <generator class="native">
            </generator>
        </id>

        <property
            name="title"
            type="java.lang.String"
            update="true"
            insert="true"
            column="TITLE"
            not-null="true"
        />

        <set
            name="channels"
            table="CAT_GROUP_CHANNEL"
            lazy="true"
            inverse="false"
            cascade="none"
            sort="unsorted"
        >

              <key
                  column="GROUP_ID"
              />

              <many-to-many
                  class="de.nava.informa.impl.hibernate.Channel"
                  column="CHANNEL_ID"
                  outer-join="auto"
              />

        </set>

        <many-to-one
            name="parent"
            class="de.nava.informa.impl.hibernate.ChannelGroup"
            cascade="none"
            outer-join="auto"
            update="true"
            insert="true"
            column="PARENT_ID"
        />

        <bag
            name="children"
            lazy="true"
            inverse="false"
            cascade="none"
            order-by="CHANNEL_GROUP_ID"
        >

              <key
                  column="PARENT_ID"
              />

              <one-to-many
                  class="de.nava.informa.impl.hibernate.ChannelGroup"
              />
        </bag>

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

    </class>

</hibernate-mapping>

And here is the mapping for 'Channel':

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="de.nava.informa.impl.hibernate.Channel"
        table="CHANNELS"
        dynamic-update="false"
        dynamic-insert="false"
    >

        <id
            name="intId"
            column="CHANNEL_ID"
            type="integer"
        >
            <generator class="native">
            </generator>
        </id>

        <property
            name="title"
            type="java.lang.String"
            update="true"
            insert="true"
            column="TITLE"
            not-null="true"
        />

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

        <property
            name="locationString"
            type="string"
            update="true"
            insert="true"
            column="LOCSTRING"
        />

        <property
            name="site"
            type="java.net.URL"
            update="true"
            insert="true"
            column="SITE"
        />

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

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

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

        <property
            name="formatString"
            type="string"
            update="true"
            insert="true"
            column="FORMAT"
        />

        <bag
            name="items"
            table="ITEMS"
            lazy="false"
            inverse="false"
            cascade="all"
        >

              <key
                  column="CHANNEL_ID"
              />

              <one-to-many
                  class="de.nava.informa.impl.hibernate.Item"
              />
        </bag>

        <many-to-one
            name="image"
            class="de.nava.informa.impl.hibernate.Image"
            cascade="none"
            outer-join="auto"
            update="true"
            insert="true"
            column="IMAGE_ID"
            not-null="false"
        />

        <many-to-one
            name="textInput"
            class="de.nava.informa.impl.hibernate.TextInput"
            cascade="none"
            outer-join="auto"
            update="true"
            insert="true"
            column="TEXTINPUT_ID"
            not-null="false"
        />

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

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

        <many-to-one
            name="cloud"
            class="de.nava.informa.impl.hibernate.Cloud"
            cascade="none"
            outer-join="auto"
            update="true"
            insert="true"
            column="CLOUD_ID"
            not-null="false"
        />

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

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

        <property
            name="ttl"
            type="int"
            update="true"
            insert="true"
            column="TTL"
        />

        <bag
            name="categories"
            table="CAT_CHANNEL_LINK"
            lazy="true"
            inverse="false"
            cascade="none"
        >

              <key
                  column="CHANNEL_ID"
              />

              <many-to-many
                  class="de.nava.informa.impl.hibernate.Category"
                  column="CATEGORY_ID"
                  outer-join="auto"
              />

        </bag>

        <property
            name="lastUpdated"
            type="java.util.Date"
            update="true"
            insert="true"
            column="LAST_UPDATED"
        />

        <property
            name="lastBuildDate"
            type="java.util.Date"
            update="true"
            insert="true"
            column="LAST_BUILD_DATE"
        />

        <property
            name="pubDate"
            type="java.util.Date"
            update="true"
            insert="true"
            column="PUB_DATE"
        />

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

        <property
            name="updateFrequency"
            type="int"
            update="true"
            insert="true"
            column="UPDATE_FREQUENCY"
        />

        <property
            name="updateBase"
            type="java.util.Date"
            update="true"
            insert="true"
            column="UPDATE_BASE"
        />

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

    </class>

</hibernate-mapping>


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 30, 2004 1:41 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
is equals / hascode properly implemented

_________________
Emmanuel


Top
 Profile  
 
 Post subject: hashCode and equals are correct
PostPosted: Tue May 04, 2004 12:57 pm 
Beginner
Beginner

Joined: Tue Sep 30, 2003 4:16 pm
Posts: 33
emmanuel wrote:
is equals / hascode properly implemented


I checked my code carefully and both hashCode() and equals() are correct.

To further debug this I have gathered further information. I added a test to the sequence that I showed you previously:

Code:
session = sessions.openSession()
transaction = session.beginTransaction();

// new line
if (!group.channels.contains(channel) ERROR!!!

group.remove(channel);
transaction.commit();
session.flush();
session.close();


Recall that channels is a Set.

If I single step through this, I note the following:

- I can verify that the hashCode for channel equals the hashCode for the first non-null element of channels.
- the channels.contains() call is actually implemented by net.sf.hibernate.collection.Set
- Which in turn calls java.util.HashSet.contains()
- Which in turn calls java.util.HashMap.contains()
- Which FAILS to find the identical entry.

- There is also a hint of evidence of a debug information mismatch, in that my debugger says that the argument to HashMap.contains is called arg0 but the source that I look at says that that argument is called key. I am not sure whether this is significant, but I double checked my paths etc and all seems correct.

Can you tell what's wrong?

Thanks!


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 06, 2004 11:09 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
Check that one
http://www.hibernate.org/109.html

_________________
Emmanuel


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