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