Class diagrams: qualified associations

Class diagrams: qualified associations

puml.online

简介 / Introduction

An ordinary class-diagram association only says “A is related to B”. But many relationships are more precise than that: A points at one specific member of a set of Bs via some key. A Shop might have many Customers, but each transaction links to exactly one of them — that key is the UML qualifier.

The specification draws it as a small box hanging off one end of the association, holding the qualifier’s name and type, e.g. customerId: long. A reader then knows the link targets “the customer located by customerId”, not “any customer”.

The two engines are far apart here. PlantUML supports the syntax natively and genuinely draws the qualifier as a small rectangle on the line; both Mermaid versions (10.9.1 in the editor, 11.4.1 in the theme) fail to parse it outright. This article builds the same “shop–customer” scenario and measures the real behaviour of both.

PlantUML 版

PlantUML puts the qualifier in square brackets after the source class name:

1
2
3
4
5
6
7
8
9
10
11
@startuml
class Shop {
+name : String
}
class Customer {
+name : String
+tier : String
}

Shop [customerId: long] "1" -- "0..*" Customer : buys
@enduml

That single line stacks three things: the qualifier [customerId: long], the multiplicities "1" / "0..*", and the relation label : buys. They don’t conflict and combine freely. The official minimal example is terser:

1
2
3
4
class class1
class class2

class1 [Qualifier] - class2

In the generated SVG the qualifier is a standalone rectangle (fill="#F1F1F1", stroke-width:0.5") attached to the source side, with the association path starting from that rectangle’s edge rather than from the class box — which is exactly the “qualifier sits at the association end” shape the UML specification asks for. The text inside is customerId: long, type included.

Points confirmed by measurement:

  • The qualifier only goes on the left (source) side. Move it to the target class and you get a 400:
    1
    Shop -- Customer [customerId: long] : buys
    The response is Syntax Error? (Assumed diagram type: class), pointing at the ] on line 4. The asymmetry is the easiest trap here.
  • It composes with the other modifiers: aggregation *--, composition, role labels, generics, interfaces — all measured passing.
    1
    2
    3
    Shop [customerId: long] "1" *-- "0..*" Customer : buys
    Shop [customerId: long] --> "- customer" Customer : buys
    class Shop<T>
  • One class can carry several differently-qualified associations, each drawing its own small box without interference.
  • The qualifier text renders verbatim: both customerId: long and customerId : long are accepted, the former rendering without the space.

Mermaid 版

Mermaid has no concept of a qualifier. Writing the bracket syntax fails with the identical error in both versions:

1
2
3
4
classDiagram
class Shop
class Customer
Shop [customerId: long] --> "0..*" Customer : buys
1
2
3
4
Parse error on line 4:
...class CustomerShop [customerId: long] -
----------------------^
Expecting 'NEWLINE', 'EOF', 'STR', 'LABEL', 'AGGREGATION', 'EXTENSION', 'COMPOSITION', 'DEPENDENCY', 'LOLLIPOP', 'LINE', 'DOTTED_LINE', got 'SQS'

Note the got 'SQS' in the message — the square bracket was lexed as an unknown token. The editor’s bundled Mermaid 10.9.1 and the theme’s 11.4.1 report exactly the same error, so this isn’t a version-specific bug: the word simply does not exist in Mermaid’s class-diagram grammar.

Three workarounds are available, in decreasing order of fidelity:

Option one: fold the qualifier into the relation label. Relation labels are free text, so the square brackets render as-is:

1
2
3
4
5
6
7
8
9
10
classDiagram
class Shop {
+String name
}
class Customer {
+String name
+String tier
}
Shop "1" --> "0..*" Customer : buys [customerId]
note for Shop "qualifier: customerId : long"

The label comes out as buys [customerId] — the qualifier’s name is there, but the visual semantics of “a little box hanging off the association” is gone, and the reader has to parse the brackets themselves.

Option two: spell it out in a note. note for Shop "..." attaches an annotation to the class, so you can state what the qualifier is. The block above uses both options: the label carries the name, the note carries the type and the explanation.

Option three: degrade it into a class member. Shop : +String customerId makes it an ordinary attribute. Semantically that shifts from “a qualifier on the association” to “Shop happens to have a property called customerId” — not equivalent, but the information isn’t lost.

Mermaid does, incidentally, support role names: Company --> "- boss +name" Employee measures clean and renders - boss +name. But a role name and a qualifier are different things — a role name says “what role does this end play”, a qualifier says “which key locates the target”. Mermaid covers the former only.

对比 / Side-by-side

Dimension PlantUML Mermaid
Qualifier syntax Native: Shop [customerId: long] -- Customer Unsupported: reports Parse error ... got 'SQS'
Qualifier position Source end (left) only; on the target end reports Syntax Error? —
Qualifier rendering A standalone grey rectangle on the association end, with the line leaving from its edge No corresponding shape
Combining with multiplicity Yes: Shop [customerId: long] "1" -- "0..*" Customer : buys Multiplicity works, but the qualifier has nowhere to go
Combining with a role name Yes: Shop [customerId: long] --> "- customer" Customer : buys Role names work: Company --> "- boss +name" Employee
Several distinct qualifiers Each association draws its own box Only distinguishable via labels or notes
Approximation Not needed Label buys [customerId] + note for Shop "..."

Three measured conclusions:

  1. Mermaid cannot express a UML qualified association at all. This isn’t a difference in syntax details — the concept isn’t in the grammar. If a design document needs “a key locates one instance out of a set” to land in the picture, Mermaid can’t deliver it.
  2. Mermaid’s best approximation is “relation label + note”. The label carries the qualifier’s name (buys [customerId]), the note carries the type and the explanation (note for Shop "qualifier: customerId : long"). Both are standard Mermaid syntax, measured passing on both versions.
  3. PlantUML’s qualifier only lives on the left. Worth remembering as a directional constraint before you write rather than after you get a 400 — in a right-to-left relationship, the qualifier belongs on whichever side actually holds the locating key.

小结 / Wrap-up

Qualified associations solve the classic “the link points at one member of a set, not the whole set” problem. Finding a directory entry by key, a customer by order number, a config by environment name — all the same pattern.

The gap between the engines is clean: PlantUML has the concept, so it has syntax, a graphic, and a directional constraint; Mermaid doesn’t have the concept, so it has neither, and you patch it with labels and notes. PlantUML’s only gotcha is that the qualifier must be written on the source side.

Practical pick: if the diagram must be strict UML, or if readers have to understand “this is a lookup by key” from the graphic alone, use PlantUML. If the diagram just needs to convey “there’s a customerId correspondence between these two” inside a document, Mermaid’s label plus note is plenty — and cheaper to maintain, since editing a label is far safer than editing a syntax that gets rendered into diagram elements.

  • Title: Class diagrams: qualified associations
  • Author: puml.online
  • Created at : 2026-12-02 01:00:00
  • Updated at : 2026-12-05 02:08:58
  • Link: https://puml.online/blog/class-diagram-qualified-association-en/
  • License: This work is licensed under CC BY-NC-SA 4.0.
On this page
Class diagrams: qualified associations