Hi there,
I would like to build a hierarchical structure of Items. An item can be a Folder or a Document. Each item except the root folder has exactly one parent folder. All very simple. Now I have two choices:
1) each item has a parent
Code:
public class Item {
/**
* @hibernate.many-to-one column="parent_id" class="Folder"
*/
public Folder getParent() { ... }
}
2) each folder has a set of children
Code:
public class Folder {
/**
* @hibernate.set table="folder_children" cascade="all-delete-orphan" lazy="true"
* @hibernate.collection-key column="item_id"
* @hibernate.collection-many-to-many column="child_id" class="Item"
*/
public Set getChildren() { ... }
}
In case one Item.getParent() works out of the box and Folder.getChildren() has to be implemented with a query. In case two the situation is reversed.
So now comes the question: which approach is preferable?
Intuitively I would favour case one but for one problem: When I retrieve a folder that is deep down in the hierarchy Hibernate would have to retrieve all the parents up to the root folder even if I don't need them. If I retrieve all the items in a folder the same would happen for all the items in that folder. Am I right?
Thanks in advance,
Markus