Guide
Generating JPA Annotations from UML Stereotypes with Tagged Values
Learn how to map UML stereotypes with tagged values to JPA annotations and maintain round‑trip consistency between model and generated Java code.
Published by Tasadduq Burney
14 May 2026, 00:12 UTC
2 min123K views0

Desired outcome
Generate Java source files that contain JPA annotations derived from UML stereotypes and tagged values, and keep the model and code in sync through round‑trip engineering.
Prerequisites
- A UML modeling tool that supports UML 2.5 profiles and can export a profile (e.g., Enterprise Architect, Eclipse Papyrus, Visual Paradigm).
- The tool’s Java code generator configured to read stereotype tagged values.
- A Java project (Maven/Gradle) where generated sources will be placed.
Procedure
- Define a UML profile that extends <<
>Class>>> with a stereotype «Entity» and adds a tagged valuetableNameof type String. - Apply the profile to your class diagram.
- On each class that should become a JPA entity, apply the stereotype «Entity» and set the
tableNametagged value to the desired table name (e.g.,users). - For primary‑key attributes, apply a stereotype «Id» (no tagged value needed) so the generator emits
@Id. - Run the tool’s Java code generator, targeting the
src/main/javafolder of your project. - Inspect the generated files: each stereotyped class should contain
@Entity(name = "users")and the primary‑key field should have@Id. - To verify round‑trip consistency, modify a tagged value in the model (e.g., change
tableNamefromuserstouser_accounts), regenerate the code, and confirm that the@Entityannotation updates automatically without manual edits.
Expected checks
- Use a diff tool (e.g.,
git diffif the project is under version control) between the code before and after regeneration; only the annotation values should change. - Run the project’s build (
mvn compileorgradle build) to ensure the generated code compiles. - Optionally execute a unit test that persists an entity to verify the table name matches the tagged value.
Recovery options
If the generated code drifts from the model (for example, because a manual edit was made), you can:
- Re‑run the generator to overwrite the file with the model‑driven version.
- If you need to preserve a manual change, copy it to a separate hand‑crafted class and extend the generated one, or adjust the profile and tagged values to reflect the desired code.
- Roll back to a previous commit in your VCS to restore the last known‑good generated state.
Limitations
- The mapping from stereotype tagged values to Java annotations is tool‑specific; a profile built in one vendor’s tool may require re‑mapping in another.
- Large numbers of tagged values or nested profiles increase the size of the .uml/.xmi file and can slow down round‑trip synchronization.
- Only annotations that the generator knows how to emit will appear; unsupported Java features must be added manually after generation.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.