UML class model how to model many to many relationship
Asked Answered
C

2

24

I have read several tutorials on what a UML model should contain and what it shouldn't. As a developer, I always think in terms of a relational data model where you could never have a many-to-many relationship between tables. Now, with a UML class model, I've read that if they don't provide added value, you could just skip the link tables.

However, I've also read a tutorial where examples were given using data inside tables and was strongly suggesting to picture each class as a simple table while modeling your class model.

I am confused, what is it now?

Cellist answered 20/8, 2009 at 13:3 Comment(1)
What context is the class model that you are considering; Business Model, Analysis Model, or Design Model?Protestantism
S
49

The "relational link table" is an implementation technique for a Many-to-Many relationship.

The relationship exists irrespective of how it's implemented.

In an object model, you have many choices for how to implement many-to-many, which may (or may not) involve an additional persistent table. It could be done lots of different ways.

The point of UML is to be able to describe the essential features of what the model really is.

You can also describe the implementation. They're separate diagrams with separate purposes. You can easily show the relational implementation with the link table. You can also show the essential model without the link table.

Here's the essential relationship

Essential

Here's the implementation of the relationship

Implemntation

Both are valid UML. The real question is "what do you need to show other people?" Essential truth or one particular implementation?

Svetlanasvoboda answered 20/8, 2009 at 13:29 Comment(1)
Why do you use a circle and a dotted line to connect Enrollment to the relationship?Magnetometer
D
13

Model it as an M:N relationship. Same as in a relationship model there will be an associate class (or link class) of some kind in code. No need to put that on the diagram unless it has attributes on top of the join attributes (much like you would generally omit a join entity from an ERD unless it had attributes that weren't foreign keys in the related entities). The link class is typically drawn as a class connected to the relationship by a dashed line.

The Enrolment join entity is a good example of this in UML 2 Class Diagrams.

Dynah answered 20/8, 2009 at 13:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.