Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DISCRIMINATOR based Multi-Tenancy with Spring Data JPA+Hibernate

I want to implement DISCRIMINATOR based Multi-Tenancy solution for Shared Schema Based Multi-Tenancy Model-common database schema for all tenants.

Technology stack

  • Hibernate 3 Global filter (I can't use EclipseLink)
  • Spring Data JPA - I want this because we are already using this one and we cant change it.

My questions are

  • How to achieve this : Spring Data JPA has no any support for GLOBAL filter so I need to add customize Spring Data JPA repository which can in-turn apply global filter for DISCRIMINATOR based solution?
  • At all, Is it possible to implement my requirement with Spring Data JPA+Hibernate 3?
like image 616
Ketan Avatar asked Mar 05 '14 14:03

Ketan


People also ask

What is the tenant discriminator model in hibernate?

Unlike the separate database and separate schema approaches, which require distinct database connections for each tenant, Hibernate’s tenant discriminator model stores tenant data in a single database and partitions records with a simple column value. We will add a column called tenant_id in all the tables.

What are the approaches to multitenancy in hibernate?

As mentioned in the official Hibernate User Guide, there are three approaches to multitenancy in Hibernate: Separate Schema – one schema per tenant in the same physical database instance Partitioned (Discriminator) Data – the data for each tenant is partitioned by a discriminator value

How to inject a tenant discriminator clause in a JPA query?

The class is defined with standard JPA and Hibernate annotations. Notably, the @FilterDef and @Filter annotations will allow us to inject a tenant discriminator clause to every SQL query generated for this entity. We will use AspectJ advice, which will set the Filter value.

Does hibernate index the Discriminator column in JPQL queries?

However, for JPQL query such as this one: Hibernate generates a SQL query which filters by the associated discriminator column (e.g. DTYPE by default): So, because we are filtering by the discriminator column, we might want to index it or include it to speed up queries.


1 Answers

Using hibernate filters you can achieve below items in our application,

  1. Multi-tenancy,
  2. Authorisation
  3. Authentication
  4. Row level authorisation

In our application we applied this and got great results and applied the same stuff to other web based products as well. For your reference go through the below link and you will get some idea on the above stuffs. http://www.jroller.com/RickHigh/entry/using_spring_aop_and_hibernate

like image 135
Hakuna Matata Avatar answered Sep 20 '22 05:09

Hakuna Matata