-->
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: Self one-to-many mapping?
PostPosted: Thu Dec 04, 2003 3:08 am 
Newbie

Joined: Thu Dec 04, 2003 2:58 am
Posts: 1
Hi folks,

I just start trying to use Hibernate, overall it's very impressed me ;-)

I have a question though, in case I want to develop a shopping cart application, and I have a Category class that contain a set of Category classes to form sub-category. How can I map this into Hibernate?

Category <1------*> Category


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 04, 2003 6:43 am 
Expert
Expert

Joined: Fri Nov 07, 2003 4:24 am
Posts: 315
Location: Cape Town, South Africa
Hi,

The reference documentation contains an example of this mapping, using Cats and Kittens where a Cat can mapped to 0 or more kittens (which are also cats).

i.e.

CAT ----(kittens)----* CAT (kittens)

The mapping (from version 2.1 of the documentation- chapter 5) is:
Code:
<hibernate-mapping>
  <class name="eg.Cat" table="CATS" discriminator-value="C">

    <id name="id" column="uid" type="long">
      <generator class="hilo"/>
    </id>

    <discriminator column="subclass" type="character"/>

    <property name="birthdate" type="date"/>
    <property name="color" not-null="true"/>
    <property name="sex" not-null="true" update="false"/>
    <property name="weight"/>

    <many-to-one name="mate" column="mate_id"/>
    <set name="kittens">
      <key column="mother_id"/>
      <one-to-many class="eg.Cat"/>
    </set>

    <subclass name="eg.DomesticCat" discriminator-value="D">

     <property name="name" type="string"/>
    </subclass>
  </class>


<class name="eg.Dog">
<!-- mapping for Dog could go here -->
</class>
</hibernate-mapping>


Check out the documentation for more details

Justin


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.