-->
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.  [ 7 posts ] 
Author Message
 Post subject: parent and only child relationship
PostPosted: Mon May 10, 2004 7:09 pm 
Newbie

Joined: Mon May 10, 2004 6:53 pm
Posts: 11
What is the best way to model a parent-child relationship where there can only be one child per parent? Additionally, the parent and child are the same class.

I would rather avoid using a collection to hold the children if possible, and it would be nice to have a bidrectional relationship, although if that is difficult, then I would have the parent control things.

Can anyone give me a pointer, (I am comfortable with hibernate, but not sure of all the best practices)?

_________________
--
Matthew Van Horn


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 10, 2004 7:10 pm 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
If the child is not an entity with shared references, use simple <component> mapping with a <parent> element for bidirectionality.

_________________
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 5:25 am 
Newbie

Joined: Mon May 10, 2004 6:53 pm
Posts: 11
Since the child is the same class as the parent, I am guessing it would have to be an entity - even though the child instance is dependent on the parent instance.

This is kind of confusing to me - would it look like this?

table:FOO
columns:
pkid int,
bar varchar(100),
parent_id int,
child_id int

Code:
public class Foo {
    String bar;
    Foo myChild;
    Foo myParent;
}

<class name="Foo" table="FOO">
    <id name="id" column="pkid" type="int">
        <generator class="native"/>
    </id>               
    <property name="bar"/>
    <component
        name="myChild"         
        class="eg.Foo" >
        <property name="bar" column="bar"/>
        <parent name="myParent"/>
    </component>
</class>



How does the component get mapped to the child_id/parent_id columns ?

_________________
--
Matthew Van Horn


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

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
Hm, yes, that is not what you want. Your object model doesn't make much sense to me, are you saying that you have a linked list? A.getA().getA().getA()... and so on? Why would you need that?

_________________
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 5:56 am 
Newbie

Joined: Mon May 10, 2004 6:53 pm
Posts: 11
I am trying to model part of an XML schema which is constructed like this.

THis particular part is for a hierarchy of organization names. For example a person works at Acme RocketSkates, Roadrunner Pursuit division, in the R&D section. The XML would look like this:

Code:
<PositionOrgName>
    <OrgType>Company</OrgType>
    <OrganizationName>Acme RocketSkates</OrganizationName>
    <PositionOrgName>
        <OrgType>Division</OrgType>
        <OrganizationName>Roadrunner Pursuit</OrganizationName>
        <PositionOrgName>
            <OrgType>Section</OrgType>
            <OrganizationName>R and D</OrganizationName>
        </PositionOrgName>
    </PositionOrgName>
</PositionOrgName>

_________________
--
Matthew Van Horn


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

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
I think you have the wrong approach. Relational models are normalized, removing redundancy, while hierarchical models are not (and therefore, they are useless for data management). There is no easy answer to your problem, you will have to read up on relational data modeling.

_________________
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 9:09 am 
Newbie

Joined: Mon May 10, 2004 6:53 pm
Posts: 11
Yeah, I think I knew where this was headed. Most of this schema translates well to a relational model, there are only a few areas where it doesn't. So I guess I will leave out support for this part of it for now, until I can come up with something decent.

Unfortunately, although most of this model deals with things that can be modeled in a rdb, there are areas like this where the aim is to have a flexible structure (accomodating a hierarchy of unknown depth and org types) and this doesn't fit well into a relational model.

It is too bad that none of the free xml databases seem able to meet the other requirements, or I might have gone that route. I guess my job will be to figure out some kind of balance between supporting this spec, and having a performant app.

_________________
--
Matthew Van Horn


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