ER diagram and persistence
Physical model derived from V1__create_users.sql and its relationship to JPA.
Actual entity–relationship diagram
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
| Column | SQL | Rule and meaning |
|---|---|---|
| id | BIGINT, PK, AUTO_INCREMENT, NOT NULL | Long in Java; stable identity used as the JWT sub. |
| name | VARCHAR(80), NOT NULL | Display name. The minimum of 2 characters and nonblank rule are validated by DTOs, not SQL CHECK. |
| VARCHAR(160), NOT NULL, UNIQUE | The application trims and lowercases it. Uniqueness is enforced by uk_users_email. | |
| password_hash | VARCHAR(100), NOT NULL | BCrypt hash, never the original password. Excluded from UserResponse. |
| role | VARCHAR(20), NOT NULL | EnumType.STRING; CHECK ck_users_role permits only USER or ADMIN. |
| enabled | BOOLEAN, NOT NULL, DEFAULT TRUE | Active status. MySQL BOOLEAN is an alias for TINYINT(1). The entity also initialises it to true. |
Versioned 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
- Spring opens the datasource using JDBC/HikariCP.
- Flyway checks its history and runs pending migrations in order.
- V1 creates app_users and records its version and checksum.
- Hibernate uses
ddl-auto: validateto verify the schema; it neither creates nor updates it. - 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
# Desde la raíz: pedirá DB_PASSWORD.
docker compose exec mysql mysql -u security_app -p security_labSHOW 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.