From 56105a8fc660c7f5039eb00d19f31a9a30805ddc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Yoann=20Rodi=C3=A8re?= Date: Wed, 10 Apr 2024 13:44:44 +0200 Subject: [PATCH] HHH-17935 Do not use the "auto-enable" feature for the tenant ID filter This filter already gets enabled automatically where relevant, and critically it's not relevant when using a "root" tenant identifier. --- .../binder/internal/TenantIdBinder.java | 2 +- .../orm/test/tenantid/TenantIdTest.java | 46 +++++++++++++++++++ 2 files changed, 47 insertions(+), 1 deletion(-) diff --git a/hibernate-core/src/main/java/org/hibernate/binder/internal/TenantIdBinder.java b/hibernate-core/src/main/java/org/hibernate/binder/internal/TenantIdBinder.java index 96df3f175d..35e0347932 100644 --- a/hibernate-core/src/main/java/org/hibernate/binder/internal/TenantIdBinder.java +++ b/hibernate-core/src/main/java/org/hibernate/binder/internal/TenantIdBinder.java @@ -59,7 +59,7 @@ public class TenantIdBinder implements AttributeBinder { "", singletonMap( PARAMETER_NAME, tenantIdType ), Collections.emptyMap(), - true + false ) ); } diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/tenantid/TenantIdTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/tenantid/TenantIdTest.java index 56dce0aad8..b8a35a7bb2 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/tenantid/TenantIdTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/tenantid/TenantIdTest.java @@ -76,6 +76,11 @@ public class TenantIdTest implements SessionFactoryProducer { public boolean validateExistingCurrentSessions() { return false; } + + @Override + public boolean isRoot(String tenantId) { + return "root".equals( tenantId ); + } } ); return (SessionFactoryImplementor) sessionFactoryBuilder.build(); } @@ -105,6 +110,47 @@ public class TenantIdTest implements SessionFactoryProducer { } ); } + @Test + public void testRoot(SessionFactoryScope scope) { + currentTenant = "root"; + scope.inTransaction( session -> { + assertEquals( 0, session.createQuery( "from Account" ).getResultList().size() ); + } ); + + currentTenant = "mine"; + Client client = new Client( "Gavin" ); + Account acc = new Account( client ); + scope.inTransaction( session -> { + session.persist( client ); + session.persist( acc ); + } ); + assertEquals( "mine", acc.tenantId ); + scope.inTransaction( session -> { + assertNotNull( session.find( Account.class, acc.id ) ); + assertEquals( 1, session.createQuery( "from Account" ).getResultList().size() ); + } ); + + currentTenant = "root"; + // Root tenants should find entities from other tenants + scope.inTransaction( session -> { + assertNotNull( session.find( Account.class, acc.id ) ); + assertEquals( 1, session.createQuery( "from Account" ).getResultList().size() ); + } ); + + // Root tenants should find entities from their own tenant + Client rootClient = new Client( "Sacha" ); + Account rootAcc = new Account( rootClient ); + scope.inTransaction( session -> { + session.persist( rootClient ); + session.persist( rootAcc ); + } ); + assertEquals( "root", rootAcc.tenantId ); + scope.inTransaction( session -> { + assertNotNull( session.find( Account.class, rootAcc.id ) ); + assertEquals( 2, session.createQuery( "from Account" ).getResultList().size() ); + } ); + } + @Test public void testErrorOnInsert(SessionFactoryScope scope) { currentTenant = "mine";