Video in progress

Should the company build a shared "persistence starter" library (base entity, auditing, Hibernate settings)? What are the risks?

For settings and small infrastructure, yes: safe defaults (open-in-view=false, batching, slow query log, metrics), an audit listener and test helpers. But a shared base entity hierarchy, a mandatory id strategy or "magic" interceptors tie services to each other and make upgrades harder.

📖 Answer

The benefit is clear: each service stops repeating the same mistakes. This is what I usually put in the starter:

spring:
  jpa:
    open-in-view: false
    properties:
      hibernate.jdbc.batch_size: 50
      hibernate.order_inserts: true
      hibernate.query.fail_on_pagination_over_collection_fetch: true
      hibernate.query.in_clause_parameter_padding: true
      hibernate.log_slow_query: 500

Plus: hooking Hikari and Hibernate metrics into Micrometer, a StatementInspector that adds the trace id to a SQL comment, a helper for checking query counts in tests, and the Testcontainers configuration.

The risky parts:

  • the base entity. A BaseEntity with id, version, createdAt, tenantId is convenient at first. Then one service needs UUIDs, another doesn't need a tenant, and a third connects to a legacy table. A change in the inheritance touches every service. I provide only a small @MappedSuperclass (the audit fields) and leave the id strategy to the service itself;
  • hidden behavior. If a shared interceptor or listener adds a "automatic" tenant filter or soft delete, the service team stops understanding their own SQL;
  • version coupling. If the starter hard-codes the Hibernate or Spring Boot version, the services are forced to upgrade at the same time. I make the starter rely on the Boot BOM and not pin versions itself;
  • ownership. If the starter has no clear owner, changelog and semver, it quickly becomes "legacy" that nobody touches.

The starter also helps with schema consistency: ddl-auto=none on prod, Flyway manages the schema, and a separate test with validate runs in CI. I make that rule a starter default.

In short: configuration and observability are shared; the domain model and mapping decisions stay in the service itself.