How to represent a many to many relationship in a uml class diagram - uml

I have a question about a UML class diagram. Do many to many relationships have a junction table like in ERD diagrams or not? it would make sense for me having a junction table to put the quantity of items in there. Here is what I have but without a junction table

Sure there is. And it's called associaton class. The notation is like this:
This is a shortcut for
The OrderItem relates Item and PurchaseOrder by adding what ever needed (quantity, rebate, etc.) to it.

Related

Are UML associations unique when there is an association class present?

I'm taking the Stanford edx course (Databases: Modeling and theory) and the lecturer mentioned this piece of information about association classes and what they imply in a diagram (in reference to an example of two classes "student" and "college" associated by a "applies to" association with the association class "appInfo" containing college application info):
Now what we're saying is that association is going to have affiliated with it a date and a decision. What we cannot describe in UML is the possibility of having more than one relationship or association between the same student and the same college.
So when we have an association, that assumes at most one relationship between two objects. So, for example, if we wanted to add the possibility that students could apply to the same college multiple times so maybe, you know, they wanted to apply for different majors, that would actually have to be captured quite differently. We'd have to add a separate class for the application information with separate relationships to the students and colleges.
but I didn't really get what she meant here, does having an association class create an automatic 1-1 coupling between two classes? or is it prohibited for two classes to have more than one relationship? or what exactly is being said here because it's a bit vague and I'm not experienced in UML data modeling.
The statement is either wrong or it’s taken out of a specific context that was not mentioned here. The reasons are:
there is no implicit one-to-one in an association; the multiplicities at both end of an association specifies if it’s a one to one, a one to many, a many to many association or anything in between.
the same applies to an association class, which is both: an association and a class at the same time.
in a many to many association, there can very well be several times a link between the same objects. In fact, in a one-to-many or many-to-many association, you need to add an explicit constraint if you want to avoid this.
More information:
in this other answer, there are some more explanation about the semantic of association classes
an here a question related to uniqueness. as you will see, it is not that obvious to avoid multiple occurrences

Thematic/Semantic tagging of associations in UML

Is it possible to use semantic tags in UML associations (among classes) to express the wider semantic nature of the relationship?
For example, consider the relation that Doctor TREATS Patient. Beyond the TREATS relation, we also want to express that it's a medical and humanitarian relation. Is it possible to somehow tag the association between Doctor and Patient with these two additional tags in UML?
Also, consider the example of a Doctor's relation with another Doctor. We want a model where a Doctor can have multiple (semantic) relations such as Professional, Socialand many other relations with another Doctor.
If you want to "tag" UML element, you should take a look at the UML Stereotype concept.
In short it is a way to add domain related information to UML element.
In the following screenshot, I defined two stereotypes respectively named "humanitarian" and "medical" and I stereotyped a UML association with both of them.
Another solution would be to define a stereotype which has two boolean properties "humanitarian" and "medical" and set their values as true when the stereotyped is applied.

Turning ER diagram to UML: Attributes on Relationships And Managing Two Relationship Sets

I'm taking a course that is going over UML diagrams. I have been assigned to change an ER diagram to a UML diagram. For the most part, I am getting the hang of it, but I have encountered two problems that are confusing to me:
How to represent an attribute of a relationship. Here is an example of what I mean.
I do not know where I would add the "Hours attribute", as in under the Employee or Project class.
Representing two difference relationships between a set of two entities. Sort of like this:
Can I have two associations connecting the classes similar to the image I showed above?
1) use an association class. It looks like a dashed line connecting a class containing an attribute to an association.
2) you can do these associations exactly the same way in UML. However, I would use one association with verb phrases as the association-end property names.

When to use UML Association Classes?

Can I improve my design on these 2 diagrams below? If so, how?
I am confused by the use of association classes in my diagrams. Should I use them?
Figure 1
Figure 2
For association classes, they make sense only if they represent associations with some behaviour and state. Look at Article'sSubject. It has nothing more than source, target and identity. You don't need a class for such a association, just use plain association, which has all those properties. There are more such unnecessary association classes in your diagrams. Another important thing when using association classes is that every instance of that association class should have unique source and target pair. For example Comment is not identifiable just by Article and User - one user may have man ycomments on one article and that is not allowed.
An association class is used to capture certain characteristics of an association between two classes. These characteristics do not belong to the classes being associated but instead belong to the relationship between the classes.

UML class model how to model many to many relationship

I have read several tutorials on what a UML model should contain and what not. 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 linktables.
However I've also read a tutorial where examples where 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?!
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
Here's the implementation of the relationship
Both are valid UML. The real question is "what do you need to show other people?" Essential truth or one particular implementation?
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.

Resources