I am trying to implement a multi-tenancy by discriminator implementation with Spring Boot and Spring Data.
I have made an abstract class to represent a multi-tenant entity. Something similar to this:
@MappedSuperclass
@FilterDefs({@FilterDef(name = "multi-tenant", parameters = {@ParamDef(name = "tenant", type = "string")})})
@Filter(name = "multi-tenant", condition = "tenant = :tenant")
public abstract class MultiTenantEntity extends GenericEntity {
@Transient
private transient String savedTenant;
@PostLoad
private void onLoad() throws Exception {
this.savedTenant = this.tenant;
onEntityModification();
}
@PrePersist
private void onPersist() {
if (getId() == null || getId().equals(0l)) {
tenant = SecurityUtil.getCurrentTenant();
}
}
@PreUpdate
@PreRemove
private void onEntityModification() throws Exception {
String currentTenant = SecurityUtil.getCurrentTenant();
if (!currentTenant.equals(tenant) || !savedTenant.equals(tenant)) {
throw new Exception();
}
}
@NotNull
private String tenant;
public String getTenant() {
return tenant;
}
}
How do I enable the multi-tenant hibernate filter globally?