What does this class diagram mean? The class diagram of reflexive association uses solid lines and arrows, but here is replaced by a hollow diamond. Does it have anything to do with recursion? What will this class diagram generate? It would be best if you can give an example. In addition, this recursive relationship should be one-to-many, how to build a table if you store a database.
This class diagram means that an instance of Unit
can be associated with several other instances of Unit
:
The hollow aggregation diamond is just a "modeling placebo": it does not change the meaning of the diagram, but just suggests that the association represents some kind of grouping.
A reflexive association means that it associates a class with itself. There is no direct relation with recursion, as this SO answer explains.
Nevertheless, recursive algorithms are good candidates to explore such associations (e.g. find all the instances of Unit
that are indirectly related to a specific instance). In a database environment the term "recursive association" is sometimes used instead of "reflexive" because of the recursive joins that are used to implement them.
Note that the 1
should probably a 0..1
because 1
means exactly 1 and this would imply having endless cycles when navigating up.
Since it's a one-to-many association, you could visualize it as a forest of trees: each Unit
instance can be the start of some branches and several trees may share common branches (nonesense: there's at max one parent).
Let's add some roles to better speak about the ends of the aggregation:
Code generation will depends on the tool and target languages. But the model with the aggregation and the model with the simple association will most probably generate exactly the same code, something like:
class Unit { // Java
private String id;
private Unit[] child; // java objects are sharable by default
private Unit parent; // unless we make it non navigable in that direction
...
}
In an RDBMS, the table would look very similar. The relational model allows to do a bidirectional link with only one column:
ID (PK) | Parent (FK, nullable)
----------------------------------
w |
u |
u1 | u
u2 | u
u3 | u
u21 | u2
u31 | u3
u32 | u3
v |
v2 | v
A self-join or a recursive CTE would allow to query the data making use of the reflexive association.