Skip to content

Chumbok/chumbok-spring-boot-starter-data-jpa-multitenancy

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

24 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Chumbok Spring Boot Data JPA Multitenancy

Single database schema multitenancy solution for Spring Data Jpa.

Build Status Appveyor Status Code Coverage LICENSE

Quality Gate Bugs Code smells Security rating Maintainability

How to use

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>

How to use this library

  • 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.

Example Entity

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;

}

Example configuration

@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");
        }
    }
}

Example repository

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface HelloRepository extends JpaRepository<Hello, String> {

}

About

Single database schema multitenancy solution for Spring Data JPA.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages