Class diagrams: associative classes
简介 / Introduction
In a class diagram, most relationships are just “lines” — draw an association between Student and Course, put a multiplicity at each end, and you’re done. But some relationships carry attributes of their own: the act of enrolling has a grade, an enrollment date, a dropped flag. The UML specification gives a standard notation for this kind of “relationship with attributes”: an associative class — model the relationship itself as a class, then connect it back to the association with a dashed line.
The two engines differ sharply here. PlantUML supports not only the generic “third class” approach but also true UML association-class notation; Mermaid can only approximate it with a third class, and some spellings throw outright. This article builds the same “student enrolls in a course” scenario in each engine and lists the differences measured against the live backends.
PlantUML 版
Start with the “third class” form that both engines support — model the enrollment as an ordinary class and mark its identity with the <<association>> stereotype:
1 | @startuml |
A few points:
- The stereotype goes on the class declaration line:
class Enrollment <<association>> { ... }, and it renders as«association»above the class name. - Members are written name-first, type-second:
+grade : int; methods read+drop() : void. - Multiplicity and relation labels are spelled exactly as in Mermaid:
Student "1" -- "0..*" Enrollment : enrolls.
PlantUML also has a form Mermaid does not offer at all: actually attaching the class to the association line. The syntax is one extra line, Enrollment .. (Student, Course), alongside the association statement:
1 | class Student |
That line draws exactly what the UML specification calls an associative class: a dashed line running from Enrollment to the midpoint of the Student–Course association. Not to Student, not to Course — to the segment between them, which is what the specification’s figure shows. Inspecting the generated SVG confirms a dashed-stroke path whose endpoint is anchored at the association’s midpoint coordinate.
Mermaid 版
Mermaid has no notion of an associative class, so the enrollment has to be written as a plain third class:
1 | classDiagram |
Member syntax is the reverse of PlantUML’s: Mermaid uses +int grade (type first, name second), and methods read +drop() void. Multiplicity and relation labels (: enrolls) are spelled identically in both engines.
<<association>> must sit inside the class body. Move it out to a line of its own and it errors out:
1 | classDiagram |
The measured error is Cannot read properties of undefined (reading 'annotations') — Mermaid’s parser blows up while handling a class-level stereotype annotation, which is not the same thing as “the syntax isn’t recognized”.
Mermaid likewise has no way to attach a class to an association line:
1 | classDiagram |
That reports Lexical error on line 6. Unrecognized text., with the error position pointing right at the .. characters.
Mermaid’s class diagram does, however, have two things PlantUML needs extra syntax for: note for Enrollment "associative class" attaches a note to a class, and direction LR steers the layout of the whole diagram.
对比 / Side-by-side
| Dimension | PlantUML | Mermaid |
|---|---|---|
| Associative class (class attached to the association line) | Supported: Enrollment .. (Student, Course) draws the dashed link to the association’s midpoint |
Not supported: reports Lexical error on line 6. Unrecognized text. |
| Associative class (third-class form) | Supported: class E <<association>> { ... } |
Supported, but the stereotype must live inside the braces |
| Stereotype placement | On the declaration line: class E <<association>> |
Inside the body: class E { <<association>> ... }; moving it out reports Cannot read properties of undefined (reading 'annotations') |
| Member ordering | +grade : int (name first) |
+int grade (type first) |
| Layout direction | left to right direction |
direction LR |
| Class note | note bottom of E : associative class |
note for E "associative class" |
Three gotchas that will bite you in practice:
- Mermaid cannot express a UML associative class. This is the headline conclusion. If a design document requires the “relationship class hangs off the relationship line” notation, Mermaid cannot deliver it — switch to PlantUML, or fall back to a third class with an
<<association>>stereotype and explain in prose that it stands in for an associative class. - Mermaid’s stereotype must live inside the class body. Writing it outside doesn’t get ignored; it throws and takes the entire diagram down with it.
- The direction keyword differs between engines: PlantUML uses
left to right direction(after@startuml), Mermaid usesdirection LR(as the first line afterclassDiagram). Both were measured to work — with the keyword in place, the diagram’s viewBox flips from a tall box (about 132 × 344) to a wide one (about 484 × 94).
小结 / Wrap-up
Associative classes solve the “the relationship itself needs attributes” problem — an enrollment has a grade and a date, a project has a start date, an end date, and a budget. The two engines treat the two spellings very differently: PlantUML can draw the association class as the UML specification intends it (a dashed line to the midpoint of the association line) as well as approximate it with a stereotype; Mermaid has only the approximation, and the stereotype has to be tucked inside the class body.
The practical trade-off is clear: if the diagram must be strict UML, or if the point of the picture is to convey that the relationship has attributes of its own, use PlantUML; if the diagram is just embedded in a document and Mermaid’s rendering is more convenient, use Mermaid and accept that the associative class degrades into an ordinary class. Either way, give the class an <<association>> stereotype or a note — otherwise readers will take it for a class that appeared from nowhere.
- Title: Class diagrams: associative classes
- Author: puml.online
- Created at : 2026-12-01 01:00:00
- Updated at : 2026-12-05 02:08:58
- Link: https://puml.online/blog/class-diagram-associative-class-en/
- License: This work is licensed under CC BY-NC-SA 4.0.