A persistence model represents data in the form an ORM can store and retrieve. Keep it separate from the domain aggregate: the model can meet JPA’s requirements for mutable entities and no-argument constructors without weakening the aggregate’s invariants. A persistence mapper translates between the two. section 9.5.1
Keep the boundary explicit:
- Shape the persistence model for storage, not as a second domain aggregate. section 9.5.1
- Translate between domain and persistence types in the adapter’s mapper; do not make a framework-level repository stand in for the domain port. section 9.5.1
Listing 9.22 JpaArticle: The persistence model, separate from aggregate
@Entity #A
@Table(name = "articles")
@Getter
@Setter #B
@NoArgsConstructor(access = AccessLevel.PUBLIC) #C
public class JpaArticle {
@Id
@Column(name = "id", nullable = false, updatable = false)
private String id; #D
@Column(name = "title", nullable = false)
private String title;
@Column(name = "content", nullable = false, length = 10_000)
private String content;
@Column(name = "author_id", nullable = false)
private String authorId;
@Column(name = "status", nullable = false)
private String status;
public JpaArticle(final String id, final String title,
final String content, final String authorId,
final String status) { #E
this.id = id;
this.title = title;
this.content = content;
this.authorId = authorId;
this.status = status;
}
}
A JPA entity stores an article as mutable primitive fields, separate from the domain aggregate. section 9.5.1
