-->
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: How do I model this?
PostPosted: Mon Jul 02, 2007 4:39 pm 
Newbie

Joined: Mon Mar 26, 2007 5:26 am
Posts: 10
Hibernate version:
3.2

Hey all

I have an assignment where I have to model a simple database with
pupils. Each pupil must in turn have a 1-n relationship to other
pupils (the friends table). So far I have this model:

**********
* pupils *
**********
* id *
* other fields omitted...
**********

****************
* pupils_friends*
****************
* id *
* friend_id *
* pupil_id *
* type_of_relationship *
****************

Code:
<class name="com.someapp.model.Pupil" table="pupils" >
   <id name="id" column="id" type="java.lang.Long">
      <generator class="increment"/>
   </id>
   <bag name="friends" lazy="false">
      <key column="user_id" />
      <one-to-many class="com.someapp.model.Friend" />
   </bag>
</class>


Code:
<class name="com.someapp.model.Friend" table="pupils_friends" >
   <id name="id" column="id" type="java.lang.Long">
      <generator class="increment"/>
   </id>
<many-to-one
      name="pupil"
      column="pupil_id"
      not-null="true"
/>
<many-to-one
      name="friend"
      column="friend_id"
      not-null="true"
/>
</class>


In my Hibernate mapping each Pupil domain object has a list of PupilFriend objects. To retrieve a list of friends for a pupil you thus have to first iterate over each PupilFriend object and then for each such object get the friend - which probably is the result of a awkwardly designed database model.

In my case I'd like to have a List<Pupil> for each Pupil object, is this somehow mappable?

Thanks!


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 03, 2007 2:57 am 
Senior
Senior

Joined: Thu May 17, 2007 2:31 am
Posts: 194
Location: Sri Lanka
Hi

You can use many-to-many mapping in set element

Code:
<set
            name="friends"
            table="Pupil_Friend"
            lazy="false"
            cascade="none"
            sort="unsorted"
        >

            <key
                column="Pupil_Id"
            >
            </key>

            <many-to-many
                class="lk.wts.test.Pupil"
                column="Friend_Id"
                outer-join="auto"
             >   
             
             </many-to-many>      
        </set>



Amila

(Don't forget to rate if helps)


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.