liveBook · Manning

Persistence model

As explained in Clean Applications with Hexagonal Architecture

A persistence model is a representation of data shaped for storage and retrieval by a persistence mechanism.

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

How Clean Applications with Hexagonal Architecture covers it

How deep it goes
Shows a JPA entity, mapper, and repository adapter translating between domain objects and persistence types.
Examples
Uses JpaArticle with primitive article fields, a no-argument constructor, and a mapper to and from Article.
What it assumes
Assumes a domain aggregate and repository port whose types differ from the persistence entity and framework repository.
What you can do after
Build a separate JPA entity and mapper to preserve domain invariants across the storage boundary.
How it connects
Connects domain aggregates, persistence adapters, repository ports, JPA, and Spring Data repositories.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Clean Applications with Hexagonal Architecture. The section links point to the parts of the book it draws on.