如何使用 Spring Data Jpa 启用多租户 [英] How to enable Multitenancy with Spring Data Jpa

查看:83
本文介绍了如何使用 Spring Data Jpa 启用多租户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

背景:我正在构建一个多租户 SaaS 应用程序,并选择了单一数据库、共享架构作为多租户方法.每个表都有一个鉴别器列tenantId"来隔离租户数据.我使用 spring boot 作为应用程序框架,并将 spring data jpa 用于数据层,Hibernate 作为 JPA 提供程序.我真的很喜欢 Spring Data 有助于消除样板代码的方式,并且目前已经编写了如下所示的存储库,

Background : I am building a Multitenant SaaS app and have chosen single database, shared schema as the Multitenancy approach. Every table has a discriminator column "tenantId" to isolate the tenant data. I am using spring boot as the application framework and leveraging spring data jpa for the data layer with Hibernate as the JPA provider. I really like the way spring data helps in eliminating boilerplate code and have currently coded the respositories like below,

@Repository
public interface UserRepository extends JpaRepository<User,Long>{

}

和如下服务,

public class UserService{
    @Autowired
    private UserRepository userRepo;
    public User getUser(){
        User user = userRepo.findOne(id);
    }
}

问题陈述:当我想获取用户时,我想获取特定组织的用户.我想知道如何添加租户标准,我不想编写自定义存储库实现,因为这会引入样板代码.

Problem Statement: When i want to get user, i want to get the user for a particular org. I am wondering how do i add the tenant criteria, i do not want to write custom repository implementations as that will introduce boilerplate code.

尝试的解决方案:

i) Hibernate Interceptor - onPrepareStatememt :这没有用,因为 sql 是一个字符串,我不想进行字符串操作.

i) Hibernate Interceptor - onPrepareStatememt : This is not useful as the sql is a string and i do not want to do string manipulation.

ii) 使用 Spring AOP 启用 Hibernate 过滤器:使用 @Filter 注释实体并尝试在会话中设置过滤器.这不起作用,因为从不调用方面.

ii)Enabling Hibernate Filters with Spring AOP: Annotated entity with @Filter and tried setting the filter in session. This does not work as the aspect is never invoked.

@AfterReturning(pointcut = "execution(* org.hibernate.jpa.internal.EntityManagerImpl.OpenSession(..))", returning = "session")
public void forceFilter(JoinPoint joinPoint, Object session) {

    Session hibernateSession = (Session) session;
    session.enableFilter("tenantFilter")
}

休眠过滤器听起来像是一种很有前途的方法,但我在尝试提供可行的解决方案时遇到了困难.我想知道是否有替代方法可以在 Spring 数据内部用于查询数据的会话中启用休眠过滤器.

The hibernate filters sounds like a promising approach but i have kind of hit the wall trying to put in a working solution. I would like to know if there is an alternate approach to enable the hibernate filter in the session that Spring data uses internally to query the data.

推荐答案

我在博客上写了关于 使用 Spring Boot、JPA、Hibernate 和 Postgres 的多租户应用程序,尽管我采用了每个租户的 DB 方法,DISCRIMINATOR(用于指定不同租户的一个或多个表列)方法很可能需要较少的配置.

I have blogged about Multi-tenant applications using Spring Boot, JPA, Hibernate and Postgres and even though I took the DB per tenant approach, the DISCRIMINATOR (One or multiple table columns used to specify different tenants) approach you are using most-likely requires less configuration.

看看 CurrentTenantIdentifierResolver 实现 (TenantDvdRentalIdentifierResolverImpl.java),DvdRentalMultiTenantInterceptor.java Spring MVC拦截器和 DvdRentalTenantContext.java 使用 ThreadLocal 存储/传递tenantId.

Take a look at the CurrentTenantIdentifierResolver implementation (TenantDvdRentalIdentifierResolverImpl.java), the DvdRentalMultiTenantInterceptor.java Spring MVC interceptor and DvdRentalTenantContext.java that uses the ThreadLocal to store / pass the tenantId.

这篇关于如何使用 Spring Data Jpa 启用多租户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆