SPRING SECURITY / 06

ER diagram and persistence

Physical model derived from V1__create_users.sql and its relationship to JPA.

Actual entity–relationship diagram

ER diagram: app_users table
This model has one business entity, so there are no relationships between domain tables. Click to enlarge the SVG.

There is no invented roles table: each account has one role stored as text. There are also no session, permission, refresh-token or JWT tables. Flyway creates its own technical flyway_schema_history table to track migrations, independently of AppUser.

Data dictionary

ColumnSQLRule and meaning
idBIGINT, PK, AUTO_INCREMENT, NOT NULLLong in Java; stable identity used as the JWT sub.
nameVARCHAR(80), NOT NULLDisplay name. The minimum of 2 characters and nonblank rule are validated by DTOs, not SQL CHECK.
emailVARCHAR(160), NOT NULL, UNIQUEThe application trims and lowercases it. Uniqueness is enforced by uk_users_email.
password_hashVARCHAR(100), NOT NULLBCrypt hash, never the original password. Excluded from UserResponse.
roleVARCHAR(20), NOT NULLEnumType.STRING; CHECK ck_users_role permits only USER or ADMIN.
enabledBOOLEAN, NOT NULL, DEFAULT TRUEActive status. MySQL BOOLEAN is an alias for TINYINT(1). The entity also initialises it to true.

Versioned SQL

sql
-- Flyway ejecuta esta migración una sola vez y registra su versión.
CREATE TABLE app_users (
    id BIGINT NOT NULL AUTO_INCREMENT PRIMARY KEY,
    name VARCHAR(80) NOT NULL,
    email VARCHAR(160) NOT NULL,
    password_hash VARCHAR(100) NOT NULL,
    role VARCHAR(20) NOT NULL,
    enabled BOOLEAN NOT NULL DEFAULT TRUE,
    CONSTRAINT uk_users_email UNIQUE (email),
    CONSTRAINT ck_users_role CHECK (role IN ('USER', 'ADMIN'))
);

What each layer guarantees

Jakarta Validation checks email format, lengths and the BCrypt limit. UserService checks business rules and normalises values. The database guarantees NOT NULL, the primary key, unique email and the allowed roles. NOT NULL alone does not prevent an empty string; form rules do not automatically become SQL constraints.

The repository checks duplicates for clear messages, but two requests may race. The unique index therefore remains essential. GlobalExceptionHandler translates DataIntegrityViolationException to 409.

Flyway and Hibernate: different roles

  1. Spring opens the datasource using JDBC/HikariCP.
  2. Flyway checks its history and runs pending migrations in order.
  3. V1 creates app_users and records its version and checksum.
  4. Hibernate uses ddl-auto: validate to verify the schema; it neither creates nor updates it.
  5. If demo mode is enabled, the runner creates missing accounts.

To evolve the schema, create a new migration, for example V2__add_created_at.sql, and adapt the entity as needed. Do not rewrite an applied V1: changing its checksum can prevent startup. Validate each migration against a MySQL copy as well as H2 tests.

Transactions and connections

UserService uses @Transactional: a write operation is one unit of work. Read methods use readOnly=true. saveAndFlush forces changes to synchronise with the database inside the transaction; commit occurs on successful completion. HikariCP lends connections; it neither stores users nor replaces the database.

open-in-view: false avoids extending the JPA context during web serialisation. DTOs make output data explicit. MySQL persistence lives in the Docker volume, not image layers.

Inspect without revealing passwords

shell
# Desde la raíz: pedirá DB_PASSWORD.
docker compose exec mysql mysql -u security_app -p security_lab
sql
SHOW TABLES;
DESCRIBE app_users;
SELECT id, name, email, role, enabled FROM app_users ORDER BY id;
SELECT installed_rank, version, description, success
FROM flyway_schema_history ORDER BY installed_rank;

Replace security_app if you changed DB_USER. This command uses the single root compose.yml and accesses MySQL inside its container.

CV & portfolio · ES / EN
EspañolEnglish