-->
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.  [ 5 posts ] 
Author Message
 Post subject: Parent/child
PostPosted: Wed Aug 04, 2004 5:23 am 
Newbie

Joined: Fri Jul 23, 2004 4:49 am
Posts: 7
Location: France
Hello,

I'm using hibernate 2.1.4

What I would like to do is the same thing that the parent/child example in the hibernate documentation except the primary key is the id column and the parent_id (which is the foreign key).


So it makes possible to have :
Code:
  id   |   parent_id
---------------------
  0    |    1
  1    |    1
  0    |    2
  1    |    2
...



The hibernate example :
http://www.hibernate.org/hib_docs/reference/en/html/collections.html#collections-example

Is anybody can help me?
Thx.


Top
 Profile  
 
 Post subject: Re: Parent/child
PostPosted: Wed Aug 04, 2004 5:27 am 
Newbie

Joined: Fri Jul 23, 2004 4:49 am
Posts: 7
Location: France
Hello,

I'm using hibernate 2.1.4

What I would like to do is the same thing that the parent/child example in the hibernate documentation except the primary key of the child table is the id column and the parent_id (which is the foreign key).


So it makes possible to have :
Code:
  id   |   parent_id
---------------------
  0    |    1
  1    |    1
  0    |    2
  1    |    2
...



The hibernate example :
http://www.hibernate.org/hib_docs/reference/en/html/collections.html#collections-example

Is anybody can help me?
Thx.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 04, 2004 12:15 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 3:00 pm
Posts: 1816
Location: Austin, TX
Code:
    <class name="my.Entity">
        <id name="id">
            <generator class="sequence"/>
        </id>
        <many-to-one name="parent" column="parent_id" class="my.Entity"/>
        <set name="children" lazy="true">
            <key column="parent_id"/>
            <one-to-many class="my.Entity"/>
        </set>
    </class>


No different really than the example you list.

BTW, the forum does have a search. This has been asked and discussed many times before.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 04, 2004 3:18 pm 
Newbie

Joined: Fri Jul 23, 2004 4:49 am
Posts: 7
Location: France
Thank you for replying Steve.

However I am a new hibernate user and I don't understand the <many-to-one... in your sample. It seems like the my.Entity object have a parent method?

If it was already posted could you give me the link, cause I didn't find it.

I just want to add the parent_id column in the primary key of the child table. Below the code which inspired my code. I try many things but it doesn't work. In this code the PK contains only the id column in the child table.
Code:
<hibernate-mapping>
    <class name="eg.Parent">
        <id name="id">
            <generator class="sequence"/>
        </id>
        <set name="children" inverse="true" lazy="true">
            <key column="parent_id"/>
            <one-to-many class="eg.Child"/>
        </set>
    </class>

    <class name="eg.Child">
        <id name="id">
            <generator class="sequence"/>
        </id>
        <property name="name"/>
        <many-to-one name="parent" class="eg.Parent" column="parent_id" not-null="true"/>
    </class>
</hibernate-mapping>


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 04, 2004 4:32 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 3:00 pm
Posts: 1816
Location: Austin, TX
Oh, missed the composite key bit. Check out http://www.hibernate.org/hib_docs/reference/en/html/components.html#components-compositeid


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