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:
@ManyToOneand@OneToOnehave an explicitfetch = LAZY(the default is EAGER!);@OneToManyonly for a bounded collection, withmappedBy; if it is bidirectional, there areadd/removemethods that sync both sides;cascade = ALLonly inside an aggregate; no cascade on@ManyToOne;- enums use
EnumType.STRING; BigDecimalfor money, with explicitprecision/scaleon the column.
Identity and concurrency:
- the ID is a sequence (pooled) or UUIDv7,
IDENTITYonly with a reason; @Versionon 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;
clearAutomaticallyon 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.
Sign up to mark studied questions and save your progress.