When a domain object also serves as a database model, persistence annotations and storage constraints leak into the domain. A persistence entity is a separate type shaped for the ORM; a repository adapter maps between it and the domain entity. section 5.4.4
In a two-way mapping design:
- Keep the persistence entity mutable and annotated for storage; keep the domain entity free of persistence concerns. section 5.4.4
- Place the domain-to-persistence and persistence-to-domain mapping in the repository adapter. section 5.4.4
- Account for mapper upkeep as the domain evolves; distinct types improve isolation but add mapping work. section 5.4.4 section 5.4.5
JPA article record
A `JpaArticle` class uses `@Entity` and `@Table`, with an `@Id` field and article data fields. It belongs to persistence rather than serving as the domain `Article`. section 5.4.4
Listing 5.13 Two-way mapping: domain <-> persistence, domain -> DTO
@Entity #A
@Table(name = "articles")
final class JpaArticle {
@Id
private String id;
private String title;
private String content;
private String authorId;
private String status;
// getters and setters for JPA
}
The listing shows a JPA persistence entity with storage annotations and fields, distinct from the domain object. section 5.4.4
