liveBook · Manning

Persistence entity

As explained in Clean Applications with Hexagonal Architecture

An object representing data in the form and structure required by a persistence mechanism.

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

How Clean Applications with Hexagonal Architecture covers it

How deep it goes
Compares no mapping, two-way mapping, and a full bridge, including isolation and mapper costs.
Examples
Uses `JpaArticle` with JPA annotations and article fields as the persistence representation.
What it assumes
Assumes a domain entity and repository adapter, distinguishing persistence representation from domain behavior.
What you can do after
Build it from scratch as an ORM-shaped type and map it to and from the domain object in the repository adapter.
How it connects
Connects persistence entities to domain entities, repository adapters, DTOs, and mappers.

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.