Video in progress

What checklist do you give the team for reviewing a PR with JPA entities?

I check the fetch type (everything LAZY), whether cascade and orphanRemoval are justified, whether equals/hashCode/toString are safe, whether ID generation works with batching, whether there is a @Version, whether it matches the migration, and whether the new queries bring an N+1 or pagination problem.

📖 Answer

The checklist has to be short, otherwise nobody reads it. Mine goes roughly like this.

Mapping:

  • @ManyToOne and @OneToOne have an explicit fetch = LAZY (the default is EAGER!);
  • @OneToMany only for a bounded collection, with mappedBy; if it is bidirectional, there are add/remove methods that sync both sides;
  • cascade = ALL only inside an aggregate; no cascade on @ManyToOne;
  • enums use EnumType.STRING;
  • BigDecimal for money, with explicit precision/scale on the column.

Identity and concurrency:

  • the ID is a sequence (pooled) or UUIDv7, IDENTITY only with a reason;
  • @Version on entities that get updated;
  • equals/hashCode follows the safe template above, no Lombok @Data.

Schema:

  • a Flyway migration with every entity change, in expand-contract order;
  • FK columns have an index (PostgreSQL does not create them automatically).

Queries:

  • a query count test for a new list endpoint;
  • no collection fetch join + pagination;
  • clearAutomatically on bulk @Modifying;
  • the controller does not return entities.

Half of this can be automated: ArchUnit (entities must not leave the controller, no @Data), and a simple test that checks through the metamodel that all @ManyToOnes are LAZY. People then spend their time only on the design questions: aggregate boundaries, cascade logic.