All -
Thanks in advance for any help. Here goes:
I have a bunch of objects that I want to organize in tree structures, but the same node can be present multiple times in a single tree, and the same node can be present in multiple trees. That is, the nodes in the trees are really types of objects; I don't really want to treat the same type of node as two disparate objects if it is present in two trees, for example. The trees would thus look something like the following:
Code:
V S A B D E V
/ \ / \ /|\
A B C V A B C
/|\
A B D
So, in the above example diagram:
- Tree 1 has V as a parent to nodes A and B.
- Tree 2 has S as a parent to nodes C and V; V, in turn, is a parent to nodes A, B, and D.
- Tree 3 consists of a single node, A.
- Tree 4 consists of a single node, B.
- Tree 5 consists of a single node, D.
- Tree 6 consists of a single node, E.
- Tree 7 has V as a parent to nodes A, B, and C.
To store the above data, in order to avoid storing multiple rows for each type of node (for example, node A is in three trees, but I wouldn't want to store three rows) what I thought I would do is something like this:
Code:
Table NODES
id node type
----------- ------------
1 A
2 B
3 C
4 D
5 E
6 S
7 V
Table NODE_LINKS
id node_id parent_id tree_id
----------- ----------------- ----------- ----------
1 7 NULL 1
2 1 7 1
3 2 7 1
4 6 NULL 2
5 3 6 2
6 7 6 2
7 1 7 2
8 2 7 2
9 4 7 2
10 4 NULL 3
11 4 NULL 4
12 4 NULL 5
13 4 NULL 6
14 7 NULL 7
15 1 7 7
16 2 7 7
17 3 7 7
Table TREES
(consists of an id field; maybe some non-key fields, but I don't think they are germane to the example)
Any tips on how I should tackle this? Is there anything in a Hibernate mapping that I can take advantage of, or do I have to completely roll my own for everything (meaning, do I have to manually put together the NODE_LINKS table objects and save them, or there, by some chance, some sort of collection mapping I can use that will handle that for me)?
Thanks very much!