Effective hierarchy view in sleep mode

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 {
    ...
    /** @return all items in this group and its descendants */
    Set<Item> getAllItems() { ... }

    /** @return all direct children of this group */
    Set<Group> getChildren() { ... }
    ...
}

class Item {
    ...
    /** @return all groups that this Item is a direct member of */
    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;
    ...
    /** @return all items in this group and its descendants */
    Set<Item> getAllItems() {
        Set<Item> allItems = new HashSet<Item>();
        allItems.addAll(this.items);
        for(Group child : this.getChildren()) {
            allItems.addAll(child.getAllItems());
        }
        return allItems;
    }

    /** @return all direct children of this group */
    Set<Group> getChildren() {
        return this.children;
    }
    ...
}

class Item {
    ...
    private Set<Group> groups;
    /** @return all groups that this Item is a direct member of */
    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. !

+5
4

, , , . , , . , ( ) . , .

, , , .

- . -, , . , 7-10 , , - .

:

  • , . , , .
  • , , 1, .
+4

, .

( ) , . , , , - .

, (FROM tree t WHERE t.parent IS NULL), (FROM tree t WHERE t.root =:. )

" " ​​ @Transient. , .

. , , . , , (6, ) "". , .

( 1 6 " ", .)

+3

sql, node. , node id. - . . . sql-, node.

+1

All Articles