my-spring-boot-experiments
  • README
  • Spring Batch implementation
  • API Example for Archunit
  • spring-boot-chaos-monkey
  • Grafana LGTM Implementation
  • MongoDb for insertion and search using elastic search
  • OpenSearch Implementation
  • spring-boot-rabbitmq-thymeleaf
  • boot-rest-docs-sample
  • boot-strategy-plugin
  • spring-boot-togglz-sample
  • spring-boot-ultimate-redis
  • GraphQL
    • spring-boot-graphql-querydsl
    • GraphQl implementation using webflux
    • GraphQl implementation using webmvc
  • httpClients
    • http-proxy
    • rest-template
    • rest-client
    • web-client-mvc
    • web-client-webflux
  • jmh-benchmark
  • jpa
    • Custom Sequence
    • Hibernate Envers Implementation
    • multiple datasources using Spring Boot
    • spring-boot-hibernate2ndlevelcache-sample
    • JNDI in embedded Tomcat
    • JPA Jooq Marriage
    • JPA locks implementation
    • read-replica-with-spring-boot
    • KeySet Pagination Using Blaze
    • KeySet Pagination Using Data-JPA
    • MultiTenancy using Hibernate in Spring Data JPA
      • multitenancy-db
      • multidatasource-multitenancy
      • Partitioned (Discriminator) Data – the data for each tenant is partitioned by a discriminator value
      • schema
  • open-api-spring-boot
  • r2dbc
    • r2dbc-jooq
    • PostgreSQL JSON and enum column support
    • PostgreSQL Notify and Listen support using reactive spring boot
    • r2dbc-boot
    • reactive-cache
  • scheduler
    • Scheduling using JobRunr
    • Scheduling using Quartz
    • Scheduling using Shedlock distribution
  • Code Of Conduct
Powered by GitBook
On this page
  • Notes
  • Run tests
  • Run locally
  • Useful Links

Was this helpful?

Edit on GitHub
  1. jpa
  2. MultiTenancy using Hibernate in Spring Data JPA

Partitioned (Discriminator) Data – the data for each tenant is partitioned by a discriminator value

Previousmultidatasource-multitenancyNextschema

Last updated 1 year ago

Was this helpful?

Multi-tenancy with partitioned (discriminator) data is a type of software architecture in which a single instance of a software application is shared among multiple tenants, or users, who are isolated from each other through the use of a discriminator column in the data model. The discriminator column is used to differentiate and segregate the data belonging to each tenant, allowing for data security and privacy among the tenants. This type of architecture is commonly used in cloud-based software-as-a-service (SaaS) applications where multiple businesses or organizations use the same application, but have their own data and settings within the application.

Architecture Image Credit :

Notes

  • Partitioned (Discriminator) Data is supported only from Hibernate 6.x and Spring boot 3.x, each discriminator is annotated with @Tenant which will be added to the where clause automatically using TenantIdentifier Resolver. TenantIdentifierResolver.resolveCurrentTenantIdentifier() is called while creating the Hibernate session, so the tenant should be set because it is called. To set tenant we are using TenantInterceptor to fetch the tenant from request and set to TenantIdentifierResolver.setCurrentTenant() for resolving the value

  • The moment a new tenant is identified Hibernate Sequence Generator will increment its value by 50, refer ApplicationIntegrationTest class testSequenceCollision Junit

Run tests

$ ./mvnw clean verify

Run locally

$ docker-compose -f docker/docker-compose.yml up -d
$ ./mvnw spring-boot:run -Dspring-boot.run.profiles=local

Useful Links

  • Swagger UI: http://localhost:8080/swagger-ui.html

  • Actuator Endpoint: http://localhost:8080/actuator

Vlad