Relationship design problem

Hi,

I have a design problem with two classes, just say class A and class B. The relationship of A and B is 1:n, which means one A object can hold many B objects. It is a aggregation not a composition, so one B object can be hold by different A objects. Both A and B are entity classes. Here is the problem : I want display a collection of A and B in a tree structure, e.g in JTree, A is the top level node and B is the second level node, I can navigate from A to B since in A I can use collection to hold B objects, but how to navigate from B to A? e.g. how to define the getParent() method in B? since one B may have several parents A objects, but in the tree view, the upper level node is the real parent which is what I want, how to capture this A object?

Thanks. Robin

[791 byte] By [Zjga] at [2007-10-3 11:31:42]
# 1

You quite surely mix classes with instances :)

I would wonder, if an instance of B could be part of more than one instance of A, as, in this case, you would have a n:n relationship between A and B, which results in a graph, not a tree.

Your A will provide a variable of a subtype of Collection, I guess. and your B will have a container variable to take its parent instance of A.

stefan.schulza at 2007-7-15 13:58:23 > top of Java-index,Other Topics,Patterns & OO Design...
# 2

Hi, stefan

the relation is n:n, let me give a real example, consider the relationship between organizations and persons. one organization can contain several persons, and one person can belong to serveral organization.

I want to display the relation in Tree, the top level is organization and the second is person. how to store parents info in person class or instance?

Zjga at 2007-7-15 13:58:23 > top of Java-index,Other Topics,Patterns & OO Design...
# 3

Ok (you said differently in your first posting :)).

You try to display a graph as a tree, wich is quite difficult. I'd suggest to use seperate container classes (and thus instances) that hold a reference to the actual objects and only are used within the tree to represent "folders" and "leafs". folders would contain e.g. an organization and its leafs the persons.

stefan.schulza at 2007-7-15 13:58:23 > top of Java-index,Other Topics,Patterns & OO Design...