Add following dependency in your pom.xml
<dependency>
<groupId>com.chumbok</groupId>
<artifactId>chumbok-spring-boot-starter-data-jpa-multitenancy</artifactId>
<version>1.1.0</version>
</dependency>
Add add repository:
<repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>
-
Step 1: Extend entity from BaseTenantEntity class.
-
Step 2: Use @EnableMultitenantJpaRepositories annotation instead of @EnableJpaRepositories. Make use you use @EnableMultitenantJpaRepositories in your project base package. Otherwise repositories will not be scanned.
-
Step 3: Create TenantAware bean, see example below.
-
Step 4: To make sure it’s working, check for "tenantEntityListener, tenantHandler and tenantAspect bean definitions are created." and "orgFilter and tenantFilter is enabled." in the log.
-
Final step: See following example.
import com.chumbok.multitenancy.entity.BaseTenantEntity;
import lombok.Data;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
@Data
@Entity
public class Hello extends BaseTenantEntity {
@Id
private String id;
@Column
private String message;
}
@Configuration
// Make sure @EnableMultitenantJpaRepositories in class located in project base package.
// Maybe use in Application.java class if it located in project base package.
@EnableMultitenantJpaRepositories
public class MultiTenantJpaConfig {
@Bean
public TenantAware<String> tenantAware() {
return new TenantAwareImpl();
}
static class TenantAwareImpl implements TenantAware<String> {
@Override
public Optional<String> getOrg() {
return Optional.of("TestOrg");
}
@Override
public Optional<String> getTenant() {
return Optional.of("TestTenant");
}
}
}