我已经使用SCHEMA方法成功设置了多租户spring应用。
这是我的持久性配置
@Configuration
@EnableJpaRepositories(basePackages = "com.pdp")
@EnableJpaAuditing(auditorAwareRef = "auditorProvider")
public class PlatformPersistenceConfig {
@Bean
AuditorAware<String> auditorProvider() {
return new AuditorAwareImpl();
}
@Autowired
private JpaProperties jpaProperties;
@Bean
public JpaVendorAdapter jpaVendorAdapter() {
return new HibernateJpaVendorAdapter();
}
@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory(
DataSource dataSource,
MultiTenantConnectionProvider multiTenantConnectionProvider,
CurrentTenantIdentifierResolver tenantIdentifierResolver) {
LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
em.setDataSource(dataSource);
em.setPackagesToScan(MyApp.class.getPackage().getName());
em.setJpaVendorAdapter(this.jpaVendorAdapter());
Map<String, Object> jpaPropertiesMap = new HashMap<>( jpaProperties.getProperties());
jpaPropertiesMap.put(Environment.MULTI_TENANT, MultiTenancyStrategy.SCHEMA);
jpaPropertiesMap.put(Environment.MULTI_TENANT_CONNECTION_PROVIDER, multiTenantConnectionProvider);
jpaPropertiesMap.put(Environment.MULTI_TENANT_IDENTIFIER_RESOLVER, tenantIdentifierResolver);
em.setJpaPropertyMap(jpaPropertiesMap);
return em;
}
}
我与通用表(例如货币,语言环境,国家/地区等)共享架构(公共)。
每个租户的架构。
我的数据库如下
public
-- currency
-- locale
-- tenant_list
.....
foot_tenant
-- order
-- user
.....
这是我的货币实体
@Entity
@Setter
@Getter
@Table(name = "currency", catalog = "public")
public class CurrencyJPA implements Serializable {
@Id
@Column(name = "currency_code")
private String currencyCode;
.......
}
这是我的订单实体
@Table(name = "order")
public class OrderJPA extends AuditableJPA implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@ManyToOne
@JoinColumn(name = "currency_code")
private Currency currency;
.....
}
问题处于休眠状态,同时忽略了目录和架构属性
@Table(name = "currency", catalog = "public")
并抛出表foot_tenant.currency not found exception
。
谢谢