I’m building a multi-tenant Spring Boot application where all tenants share the same database and schema, but data is separated by a hospital_id column in each table.
Here’s my setup:
- Same database
- Same schema
- Tables have a hospital_id column
I want to automatically filter data per tenant (hospitalId) without adding WHERE hospital_id = :hospitalId manually in every query.
@Entity
@FilterDef(name = "hospitalFilter", parameters = @ParamDef(name = "hospitalId", type = "string"))
@Filter(name = "hospitalFilter", condition = "hospital_id = :hospitalId")
public class PatientEntity {
@Id
private Long patientId;
@Column(name = "hospital_id")
private String hospitalId;
// other fields
}
I tried using Hibernate filter, but I think this is not working with jpa repo.