-->
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: about parent-children relationship
PostPosted: Fri Feb 20, 2004 3:22 am 
Beginner
Beginner

Joined: Thu Dec 04, 2003 3:47 am
Posts: 31
Location: Hong Kong
Hi,

I have a parent class P which is associated with one-to-many relationship with a children class C (children on the many side)

Here are the partial class P and class C codes

Code:
public class P implements Serializable {
private int PID;
private Set thechildrens;

    public getPID() {
        return PID;
    }

    public Set getThechildrens() {
        return thechildrens;
    }

    public int hashCode() {
        return new HashCodeBuilder()
            .append(getPID())
            .toHashCode();
    }
}

public class C implements Serializable {
private int CID;
private P theparent;

    public getCID() {
        return CID;
    }

    public getTheparent() {
        return theparent;
    }

    public int hashCode() {
        return new HashCodeBuilder()
            .append(getCID())
            .toHashCode();
    }
}


Here are the mapping



Code:
<class
    name="P"
    table="P"
>

    <id
        name="PID"
        type="int"
        column="PID"
        unsaved-value="0"
    >
        <generator class="identity" />
    </id>

<!-- bi-directional one-to-many association to C-->
    <set
        name="thechildren"
        lazy="true"
        inverse="true"
        cascade="save-update"
    >
        <key>
            <column name="PID" />
        </key>
        <one-to-many
            class="C"
        />
    </set>

</class>

<class
    name="C"
    table="C"
>

    <id
        name="CID"
        type="int"
        column="CID"
        unsaved-value="0"
    >
        <generator class="identity" />
    </id>
<!-- bi-directional many-to-one association to P-->
    <many-to-one
        name="P"
        class="P"
        not-null="true"
    >
        <column name="PID" />
    </many-to-one>
</class>



My problem is, I use a HashSet to store the references to different children object in the parent object, using the following code

Code:
P parent = new P();
P.setThechildren(new HashSet());

C children1 = new C();
children1.setParent(P);
C children 2 = new C();
children2.setParent(P);
P.getThechildren().add(children1);
P.getThechildren().add(children2);


Then I persist parent. I suppose all "parent", "children1", "children2" would be persisted because of the save-update cascade. It turns out that only "parent", "children1" are persisted, and "parent" only has one child which is "children1"

When I debug, I find that it is because the function hashCode() in class C use CID to return the hashcode. Before being persisted, CID of object children1 and children2 has not been initialized yet, and they have the same unsaved value as 0. Therefore, these 2 objects return the same hashcode, when putting them to the Set thechildrens in object P, only 1 of them have been inserted.

So if the key of objects in class C has to be generated from the database (being "identity"), and there are no other fields that can distinguish two different objects in C, how can I add them to the set maintained in the parent class (that is P)? Maybe there are some simple ways or workaround, but I can't think of them. I think it may be a common problem others may have experienced and solved. So any one can give me some ideas? Many thank. I used Hibernate 2.0.3, MySQL 4.0.12


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 20, 2004 4:59 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
http://www.hibernate.org/Documentation/EqualsAndHashCode

_________________
Emmanuel


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.