I'm new to Multi Tenant Apps now I am exploring to to partition data based on Tenant id as far as I seen all Entity have @TenantId
as string
like this:
@Entity
public class Table {
@TenantId
private String tenant;
}
Can we have it as Tenant
object it self like:
@Entity
public class Tenant {
@Column(nullable = false)
@Id
@GeneratedValue(strategy = GenerationType.UUID)
private String id;
}
And:
@Entity
public class Table {
@TenantId
@ManyToOne
@JoinColumn(name = "tenant_id", nullable = false)
private Tenant tenant;
}
Since I want to delete all tenant related rows when an tenant gets deleted is there any way to do it? Also It is not necessary to have tenant object itself in every entity (except some) mostly I don't need except for partitioning so even adding foreign key to it may be enough. How to do it? is there anything I'm missing?
I'm new to Multi Tenant Apps now I am exploring to to partition data based on Tenant id as far as I seen all Entity have @TenantId
as string
like this:
@Entity
public class Table {
@TenantId
private String tenant;
}
Can we have it as Tenant
object it self like:
@Entity
public class Tenant {
@Column(nullable = false)
@Id
@GeneratedValue(strategy = GenerationType.UUID)
private String id;
}
And:
@Entity
public class Table {
@TenantId
@ManyToOne
@JoinColumn(name = "tenant_id", nullable = false)
private Tenant tenant;
}
Since I want to delete all tenant related rows when an tenant gets deleted is there any way to do it? Also It is not necessary to have tenant object itself in every entity (except some) mostly I don't need except for partitioning so even adding foreign key to it may be enough. How to do it? is there anything I'm missing?
Share Improve this question edited Feb 4 at 5:34 srilakshmikanthanp asked Feb 4 at 3:36 srilakshmikanthanpsrilakshmikanthanp 2,3992 gold badges13 silver badges34 bronze badges1 Answer
Reset to default 0There are few strategies for Multiple-tenant with Spring Boot and Hibernate tenant feature.
You follow the strategy what temporary (Spring team) called "partitioned table" as see at https://spring.io/blog/2022/07/31/how-to-integrate-hibernates-multitenant-feature-with-spring-data-jpa-in-a-spring-boot-application#example-1-partitioned-data . In this strategy, tenantId put inside per table.
You want
want to delete all tenant related rows when an tenant gets deleted
The key point at
@Autowired
TenantIdentifierResolver currentTenant;
in this file
@SpringBootTest
@TestExecutionListeners(listeners = {DependencyInjectionTestExecutionListener.class})
class ApplicationTests {
static final String PIVOTAL = "PIVOTAL";
static final String VMWARE = "VMWARE";
@Autowired
Persons persons;
@Autowired
TransactionTemplate txTemplate;
@Autowired
TenantIdentifierResolver currentTenant;
@Test
void saveAndLoadPerson() {
Person adam = createPerson(PIVOTAL, "Adam");
Person eve = createPerson(VMWARE, "Eve");
assertThat(adam.getTenant()).isEqualTo(PIVOTAL);
assertThat(eve.getTenant()).isEqualTo(VMWARE);
currentTenant.setCurrentTenant(VMWARE);
assertThat(persons.findAll()).extracting(Person::getName).containsExactly("Eve");
currentTenant.setCurrentTenant(PIVOTAL);
assertThat(persons.findAll()).extracting(Person::getName).containsExactly("Adam");
}
private Person createPerson(String schema, String name) {
currentTenant.setCurrentTenant(schema);
Person adam = txTemplate.execute(tx ->
{
Person person = Persons.named(name);
return persons.save(person);
}
);
assertThat(adam.getId()).isNotNull();
return adam;
}
}
source: https://spring.io/blog/2022/07/31/how-to-integrate-hibernates-multitenant-feature-with-spring-data-jpa-in-a-spring-boot-application#example-1-partitioned-data
You must: Delete all rows what related to tenant first. Then delete tenant (a small number of tenant, you can delete manually).
For simple, you can use SQL queries for deleting, it is easiest way.
DELETE FROM foo where tenantId = 'd3dd94d7-9603-441c-90dc-af87e6f3f8c9'
DELETE FROM bar where tenantId = 'd3dd94d7-9603-441c-90dc-af87e6f3f8c9'
or by source code, you create an REST endpoint, where passed to UUID tenant_id, then trigger/call repositories's method for deleting (in correct order if you use many Foreign keys. For example, 1 department has many employees, you must delete employee table first, then delete department table later.