Trouble using JPA with TenantID column in Spring Boot

I’m trying to create a multi-tenancy system for my CRM system. Basically every company using the software uses the same tables (edited from typo “every company has their own sets of tables”). The tenants table itself should be normal, without any tenant discrimination.
I would like to separate the data using a TenantID Column.
The flow would be like that

Sign in

  • User Signs in
  • gets JWT token

User adds new Meeting

  • User sends request
  • Controller receives request with JWT
  • Controller sends addMeeting to MeetingService
  • MeetingService extract uid from token
  • MeetingService fetches the corresponding tenant and tenantID
  • MeetingService sets the TenantID in a request scoped TenantContext bean
  • MeetingService fetches all Potentially conflicting Meetings (hopefully using the TenantID to find only meetings of the given tenant)
  • If no conflict remains, a new meeting is added with the proper tenant id set

There should also be a handful of endpoints and tables that don’t use the tenantID at all

Here’s what I have so far

import java.util.Map;
@Component
@RequiredArgsConstructor
//This class is supposed to set the tenantContextString in JPA
public class TenantIdentifierResolver implements CurrentTenantIdentifierResolver<String>, HibernatePropertiesCustomizer {

    TenantContext tenantContext=null;



    @Override
    public String resolveCurrentTenantIdentifier() {
        if(tenantContext.getCurrentTenant()==null)
            throw new IllegalStateException("No tenant set");

        return tenantContext.getCurrentTenant();
    }

    @Override
    public void customize(Map<String, Object> hibernateProperties) {
        hibernateProperties.put(AvailableSettings.MULTI_TENANT_IDENTIFIER_RESOLVER, this);
    }

    @Override
    public boolean validateExistingCurrentSessions() {
        return false;
    }
}



@Component
@RequestScope
//The controller can set the appropriate TenantContext for each request
public class TenantContext  {
    @Getter @Setter
    private String currentTenant=null;
}

Here’s my hibernate config

spring.jpa.properties.hibernate.multiTenancy = DISCRIMINATOR
spring.jpa.properties.hibernate.tenant_identifier_resolver =  com.polaris.polaris.auth.tenant.TenantIdentifierResolver

But this gives me this error

Error creating bean with name 'scopedTarget.tenantContext': Scope 'request' is not active for the current thread; consider defining a scoped proxy for this bean if you intend to refer to it from a singleton

I tried making the bean optional but it crashes as soon as I access it. also resolveCurrentTenantIdentifier is ran multiple times as soon as the application boots for some reason (which is probably the point at which the scope request tenantContext bean is unavailable). What is it used for and why can’t I skip it? How can I realize the plan laid out above?

2

You have said two mostly-conflicting things:

  1. every company using the software has their own sets of tables“, and
  2. I would like to separate the data using a TenantID Column“.

JPA does not provide for any concrete entity class to be mapped to different tables in the same persistence unit. Thus, your main alternatives would be:

  • Use entity inheritance with the table-per-subclass strategy for substantially all your entities, giving each tenant its own collection of entity subclasses.

    This does not scale well, and it probably incurs a performance penalty. Maybe it could work for you if you don’t anticipate many tenants, but that’s a mess I would not be eager to create for myself.

    OR

  • Use a different persistence unit, with all its own tables, for each tenant.

    The easiest way to do this would probably be to stand up a separate instance of the whole application for each tenant. They can share the same DB if you like. You can of course provide a common front end to handle sign in and redirection to the correct application instance. And inasmuch as you want to isolate tenants from each other, doing it with distinct application instances gives you even better isolation than just giving each tenant its own tables. And perhaps use a separate VM or container for each instance to reduce hardware requirements and improve isolation even more.

4

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật