Add rls-safe-mode module: Hibernate 8.0.0.CR2 HQL safe mode and PostgreSQL row-level security for @TenantId

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_017ic4b6ZEVHGaYy15HAxRzy
This commit is contained in:
Ankur Mhatre
2026-12-01 08:08:31 +05:30
co-authored by Claude Sonnet 5.5
parent 54c6d606ff
commit 1c9d9423c0
20 changed files with 817 additions and 1 deletions
@@ -0,0 +1,23 @@
package com.ankurm.tenant;
import org.hibernate.boot.model.FunctionContributions;
import org.hibernate.boot.model.FunctionContributor;
import org.hibernate.type.StandardBasicTypes;
/**
* The allow-list. With {@code hibernate.query.safe_mode_enabled=true} HQL may only call functions the
* dialect or a contributor registered. This contributor registers PostgreSQL's {@code initcap} and
* nothing else. It is discovered through META-INF/services.
*/
public class AppFunctions implements FunctionContributor {
@Override
public void contributeFunctions(FunctionContributions contributions) {
var string = contributions.getTypeConfiguration().getBasicTypeRegistry().resolve(StandardBasicTypes.STRING);
contributions.getFunctionRegistry()
.namedDescriptorBuilder("initcap")
.setExactArgumentCount(1)
.setInvariantType(string)
.register();
}
}
@@ -0,0 +1,38 @@
package com.ankurm.tenant;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.Table;
import org.hibernate.annotations.TenantId;
/**
* A tenant-owned row. {@code @TenantId} makes Hibernate (1) add {@code tenant_id = ?} to every HQL
* query and (2) in Hibernate 8, generate a PostgreSQL row-level-security policy on the table.
*/
@Entity
@Table(name = "invoice")
public class Invoice {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
public Long id;
@TenantId
@Column(name = "tenant_id", nullable = false)
public String tenant;
public String customer;
public int cents;
public Invoice() {
}
public Invoice(String customer, int cents) {
this.customer = customer;
this.cents = cents;
}
}
@@ -0,0 +1,38 @@
package com.ankurm.tenant;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import jakarta.persistence.Table;
import org.hibernate.annotations.SQLDelete;
import org.hibernate.annotations.SQLUpdate;
import org.hibernate.annotations.TenantId;
/**
* Custom SQL for update and delete. Hibernate runs the SQL as written and adds no tenant predicate, so
* the statement must carry one. Hibernate 8 lets it declare one extra trailing {@code ?} that is bound
* to the session tenant (or to null for a root tenant).
*/
@Entity
@Table(name = "receipt")
@SQLUpdate(sql = "update receipt set memo=? where id=? and tenant_id=coalesce(?,tenant_id)")
@SQLDelete(sql = "delete from receipt where id=? and tenant_id=coalesce(?,tenant_id)")
public class Receipt {
@Id
public Long id;
@TenantId
@Column(name = "tenant_id", nullable = false)
public String tenant;
public String memo;
public Receipt() {
}
public Receipt(Long id, String memo) {
this.id = id;
this.memo = memo;
}
}
@@ -0,0 +1,33 @@
package com.ankurm.tenant;
import org.hibernate.context.spi.CurrentTenantIdentifierResolver;
/** Thread-bound tenant. {@code "admin"} is the root tenant: it may see every tenant's rows. */
public class TenantContext implements CurrentTenantIdentifierResolver<String> {
public static final String ROOT = "admin";
private static final ThreadLocal<String> CURRENT = new ThreadLocal<>();
public static void set(String tenant) {
CURRENT.set(tenant);
}
public static void clear() {
CURRENT.remove();
}
@Override
public String resolveCurrentTenantIdentifier() {
return CURRENT.get();
}
@Override
public boolean validateExistingCurrentSessions() {
return false;
}
@Override
public boolean isRoot(String tenantId) {
return ROOT.equals(tenantId);
}
}
@@ -0,0 +1 @@
com.ankurm.tenant.AppFunctions