I'm having trouble presenting a hierarchy of objects in Hibernate. I searched around and could not find a single example doing this or the like - you have your apologies if this is a general question.
I have two types that I would like to use with Hibernate: Groups and Items.
* Groups are uniquely identified by a combination of name and parent.
* Groups are located in several trees, so each group has zero or one parent group.
* Each item can be a member of zero or more groups.
Ideally, I need a bi-directional relationship that allows me to get:
* all groups in which an element is a member
* all elements that are members of a particular group or its descendants.
I also need to be able to move the group tree on top to display it in the user interface.
The basic structure of the object will look like this:
class Group {
...
Set<Item> getAllItems() { ... }
Set<Group> getChildren() { ... }
...
}
class Item {
...
Set<Group> getGroups() { ... }
...
}
Initially, I just made a simple bi-directional many-to-many relationship between elements and groups, so fetching all the elements in the group hierarchy required recursion down the tree, and fetching groups for the element was a simple method of getting, i.e.:
class Group {
...
private Set<Item> items;
private Set<Group> children;
...
Set<Item> getAllItems() {
Set<Item> allItems = new HashSet<Item>();
allItems.addAll(this.items);
for(Group child : this.getChildren()) {
allItems.addAll(child.getAllItems());
}
return allItems;
}
Set<Group> getChildren() {
return this.children;
}
...
}
class Item {
...
private Set<Group> groups;
Set<Group> getGroups() {
return this.groups;
}
...
}
. , , .
Hibernate?
- ?
:
, "", , :
/RootGroup
/RootGroup/aChild
/rootGroup/aChild/aGrandChild
group_path item_id.
, :
1. ββ .
2. , group_item, group_path = 'N' group_path, 'N/%'
, , Hibernate. !