具有多租户休眠的Spring-Data JPA [英] Spring-Data JPA with Multi-Tenancy Hibernate
问题描述
我试图让Spring-Data JPA与Hibernate一起使用自定义的MultiTenantConnectionProvider。
以下配置中的所有内容似乎都起作用。每次尝试调用Repository方法时,都会调用我的 MultiTenantConnectionProviderImpl
类。
主要问题是没有办法提供租户标识符。 Spring-Data提供的Repository接口负责获取Hibernate Session。
有没有办法给Spring-Data提供租户标识符?或者是有什么地方我们可以拦截Hibernate Session的创建,所以我们可以适当地调用
sessionFactory.withOptions()。tenantIdentifier(itendintifier).openSession();
这是我的Spring配置XML文件。
<?xml version =1.0encoding =UTF -8\" >?;
< beans xmlns =http://www.springframework.org/schema/beans
xmlns:xsi =http://www.w3.org/2001/XMLSchema-instance
xmlns:context =http://www.springframework.org/schema/context
xmlns:tx =http://www.springframework.org/schema/txxmlns:jpa = http://www.springframework.org/schema/data/jpa
xsi:schemaLocation =http://www.springframework.org/schema/beans
http://www.springframework。 org / schema / beans / spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context。 xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd http://www.springframework .org / schema / data / jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd\">
< context:annotation-config />
< context:component-scan base-package =com.company/>
< jpa:repositories base-package =com.company.repositories/>
< bean id =entityManagerFactoryclass =org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean>
< property name =packagesToScanvalue =com.company.entities/>
< property name =dataSourceref =dataSource/>
< property name =jpaVendorAdapter>
< bean class =org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter>
< property name =showSqlvalue =true/>
< / bean>
< / property>
< / bean>
< bean id =transactionManagerclass =org.springframework.orm.hibernate4.HibernateTransactionManager>
< property name =sessionFactoryref =sessionFactory/>
< / bean>
< bean id =sessionFactoryclass =org.springframework.orm.hibernate4.LocalSessionFactoryBean>
< property name =hibernateProperties>
<道具>
< prop key =hibernate.dialect> org.hibernate.dialect.SQLServer2012Dialect< / prop>
< prop key =hibernate.format_sql> true< / prop>
< prop key =hibernate.multi_tenant_connection_provider> com.company.hibernate.MultiTenantConnectionProviderImpl< / prop>
< prop key =hibernate.multiTenancy> DATABASE< / prop>
< /道具>
< / property>
< / bean>
< tx:注解驱动的事务管理器=transactionManager/>
<! - 此处的供应商特定属性 - >
< bean id =dataSourceclass =org.springframework.jdbc.datasource.DriverManagerDataSource>
< property name =driverClassNamevalue =net.sourceforge.jtds.jdbc.Driver/>
< property name =urlvalue =jdbc:jtds:sqlserver:// localhost:1433 / myDatabase/>
< property name =usernamevalue =username/>
< property name =passwordvalue =password/>
< / bean>
< / beans>
CurrentTenantIdentifierResolver
< bean id =sessionFactoryclass =org.springframework.orm.hibernate4.LocalSessionFactoryBean >
< property name =hibernateProperties>
< map>
< entry key =hibernate.dialectvalue =org.hibernate.dialect.SQLServer2012Dialect>
< entry key =hibernate.format_sqlvalue =true>
< entry key =hibernate.multi_tenant_connection_providervalue =com.company.hibernate.MultiTenantConnectionProviderImpl>
< entry key =hibernate.multiTenancyvalue =DATABASE>
<! - tenant resolver as spring bean - >
< entry key =hibernate.tenant_identifier_resolvervalue-ref =currentTenantIdentifierResolver/>
< / map>
< / property>
< / bean>
< bean id =currentTenantIdentifierResolver
class =com.xxx.CurrentTenantResolver>
< / bean>
简单的租户标识符解析器就是这样的:
public class CurrentTenantResolver implements CurrentTenantIdentifierResolver {
public String resolveCurrentTenantIdentifier(){
//从登录用户获取租户
User usr =(User)SecurityContextHolder.getContext()。getAuthentication()。getPrincipal();
返回usr.getTenantName();
public boolean validateExistingCurrentSessions(){
return true;
}
}
记住上面的类是一个spring bean,所以你可以自动装配任何spring bean(service / dao),就像普通的spring bean一样。
每次spring需要session时,hibernate会从bean中检索租户标识符。
I am trying to get Spring-Data JPA working with Hibernate with a custom MultiTenantConnectionProvider.
Everything in my configuration below seems to work. My MultiTenantConnectionProviderImpl
class gets called each time I try to call a Repository method.
The main problem is that there is no way to provide a tenant identifier. The Repository interfaces provided by Spring-Data take care of getting the Hibernate Session.
Is there any way to provide Spring-Data the tenant identifier? Or is there somewhere where we can intercept the creation of the Hibernate Session so we can appropriately call
sessionFactory.withOptions().tenantIdentifier(itendintifier).openSession();
Here is my Spring configuration XML file. I tried to keep it as bare-bones as I can.
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd">
<context:annotation-config/>
<context:component-scan base-package="com.company"/>
<jpa:repositories base-package="com.company.repositories"/>
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="packagesToScan" value="com.company.entities"/>
<property name="dataSource" ref="dataSource"/>
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="showSql" value="true"/>
</bean>
</property>
</bean>
<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.SQLServer2012Dialect</prop>
<prop key="hibernate.format_sql">true</prop>
<prop key="hibernate.multi_tenant_connection_provider">com.company.hibernate.MultiTenantConnectionProviderImpl</prop>
<prop key="hibernate.multiTenancy">DATABASE</prop>
</props>
</property>
</bean>
<tx:annotation-driven transaction-manager="transactionManager"/>
<!--Vendor specific properties here-->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="net.sourceforge.jtds.jdbc.Driver"/>
<property name="url" value="jdbc:jtds:sqlserver://localhost:1433/myDatabase"/>
<property name="username" value="username"/>
<property name="password" value="password"/>
</bean>
</beans>
Use CurrentTenantIdentifierResolver
:
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="hibernateProperties">
<map>
<entry key="hibernate.dialect" value="org.hibernate.dialect.SQLServer2012Dialect">
<entry key="hibernate.format_sql" value="true">
<entry key="hibernate.multi_tenant_connection_provider" value="com.company.hibernate.MultiTenantConnectionProviderImpl">
<entry key="hibernate.multiTenancy" value="DATABASE">
<!-- tenant resolver as spring bean -->
<entry key="hibernate.tenant_identifier_resolver" value-ref="currentTenantIdentifierResolver"/>
</map>
</property>
</bean>
<bean id="currentTenantIdentifierResolver"
class="com.xxx.CurrentTenantResolver">
</bean>
Simple tenant identifier resolver would be like this:
public class CurrentTenantResolver implements CurrentTenantIdentifierResolver {
public String resolveCurrentTenantIdentifier() {
// retrieve tenant from logged in user
User usr = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal() ;
return usr.getTenantName();
}
public boolean validateExistingCurrentSessions() {
return true;
}
}
Remember the above class is a spring bean, so you can autowired any spring bean(service/dao) just like regular spring bean.
Every time spring need session hibernate will retrieve the tenant identifier from that bean.
这篇关于具有多租户休眠的Spring-Data JPA的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!