# Hibernate 8 safe mode for HQL, and PostgreSQL row-level security for multi-tenancy Runnable companion to the ankurm.com post on `hibernate.query.safe_mode_enabled` and Hibernate 8's generated PostgreSQL row-level-security policy for `@TenantId`. Every figure in the post comes from a file in [`output/`](output); each file is written by a test that asserts the same facts, so a claim that stops being true fails the build. Tested on Hibernate ORM 8.0.0.CR2 (release candidate, 24 Nov 2026), Jakarta Persistence 4.0.0-M8 (resolved transitively), PostgreSQL 16.15, pgjdbc 42.7.15, Temurin 25.0.5.1, JUnit 6.1.3, Maven 3.9.11. Both features are marked `@Incubating(since = "8.0")` in the CR2 sources. Behaviour may change before GA. ./scripts/setup-postgres.sh # once: role "app" (no superuser, no BYPASSRLS) and database "tenantdemo" ./scripts/run-all.sh # runs the tests and prints output/*.txt | File | What it shows | |---|---| | `src/main/java/com/ankurm/tenant/Invoice.java` | `@TenantId` entity | | `src/main/java/com/ankurm/tenant/Receipt.java` | `@SQLUpdate` / `@SQLDelete` with the extra tenant parameter | | `src/main/java/com/ankurm/tenant/TenantContext.java` | thread-bound tenant resolver, `admin` is the root tenant | | `src/main/java/com/ankurm/tenant/AppFunctions.java` | `FunctionContributor`: the safe-mode allow-list | | `src/main/resources/META-INF/services/org.hibernate.boot.model.FunctionContributor` | registers the contributor | | `src/test/java/com/ankurm/tenant/Lab.java` | boots a SessionFactory with safe mode and RLS toggled | | `src/test/java/com/ankurm/tenant/SqlRecorder.java` | JDBC wrapper that records SQL with bound values | | `src/test/java/com/ankurm/tenant/RlsSafeModeTest.java` | the five scenarios | | Output | Scenario | |---|---| | `output/01-safe-mode.txt` | nine HQL and Criteria calls, safe mode off and on | | `output/02-generated-rls-policy.txt`, `02b-rls-disabled.txt` | the policy Hibernate generated; nothing generated with `rls_enabled=false` | | `output/03-isolation.txt` | HQL, native SQL, raw JDBC, superuser, root tenant, no tenant | | `output/04-function-moves-tenant.txt` | a `set_config()` call from HQL moves the tenant | | `output/05-custom-sql-tenant-parameter.txt` | the tenant parameter in `@SQLUpdate` and `@SQLDelete` |