-->
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.  [ 3 posts ] 
Author Message
 Post subject: Hibernate fetching strategy in same table based on column?
PostPosted: Thu Aug 07, 2008 11:34 am 
Newbie

Joined: Thu Aug 07, 2008 9:55 am
Posts: 3
Hello every one,

I'm new to Hibernate and learning new things every week so hang on there :)

I need help about implementing a same table fetch (like a loop) for child or parent columns in the same table without going into dead loop.

what I mean is this:

I have a database Table called: FamilyTree

Code:
    _____________
   |             |
   | FamilyTree  |
   |_____________|
   |             |
   | *PERSON_ID  |
   |  PARENT_ID  |
   |  ...        |
   |_____________|



Column: PersonID
Column: ParentID

Code:
________________________
|  PERSON_ID | PARENT_ID |
|________________________|
|     1      |    -1     |
|     2      |     1     |
|     3      |     2     |
|     5      |     2     |
|     6      |     3     |
|     7      |    -1     |
|     8      |     6     |
|     9      |     5     |
|____________|___________|



PERSON 8 has these parents and grand parents (they are all FamilyTree entity) :
Code:
(8) -> 6 - > 3 - > 2 -> 1



in Other words Parent 1 has these children and grandchildren (they are all FamilyTree entity):
Code:
(1) -> 2 -> 3 -> 6 ...
        \
         5 ...
         |
         9 ...
         :         


As you can see each PersonID has a reference to its ParentID (witch in turn that FamilyTree.Parent is actually a FamilyTree entity) in the same table.

my entity should look like this:
Code:
public class FamilyTree {

    private int personID;
    private int parentID;
    private List parents;
    private List children;
   
   public List<FamilyTree> getParents() {
        return parents;
    }

    public void setParents(List<FamilyTree> parents) {
        this.parents = parents;
    }

   public List<FamilyTree> getChildren() {
        return children;
    }

    public void setChildren(List<FamilyTree> children) {
        this.children = children;
    }

    public int getPersonID() {
        return personID;
    }

    public void setPersonID(int personID) {
        this.PersonID = PersonID;
    }


    public int getParentID() {
        return parentID;
    }

    public void setParentID(int parentID) {
        this.parentID= parentID;
    }

}



Moving the Parent column to a different table is not an option here because:
1) they are all the same FamilyTree entity. the PARENT_ID is referencing to PERSON_ID in the same table.
2) I need to make it flexible so it can have ( unlimited ) sub children just like a family tree.


I hope you guys got my idea and what I'm trying to do :)
So is there a way Hibernate can implement this strategy via FamilyTree.hbm.xml?
so my mapped entity can have a List or Map instance to its children and those children have a List or Map instance to there children and so on?
Or is there a better approach then makes Hibernate work better then what I have here?

Any idea or advice is highly appreciated.

Many thanks
Anmar
p.s. Sorry for my bad English

Hibernate version: hibernate-3.2


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 07, 2008 1:50 pm 
Beginner
Beginner

Joined: Wed Jul 09, 2008 5:34 am
Posts: 41
Location: Brno, Czech Republic
Have you seen this example in the documentation?

http://www.hibernate.org/hib_docs/v3/re ... pping.html

Look at the Cat example, which maps its Mother (which is a Cat as well).


Top
 Profile  
 
 Post subject:
PostPosted: Sat Aug 09, 2008 9:59 am 
Newbie

Joined: Thu Aug 07, 2008 9:55 am
Posts: 3
thanks jpkrohling,
indeed the Cat example in the Hibernate mapping documentation was the solution for my problem :)
I have rated your reply, Many thanks.

Anmar


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