OiO.lk Blog SQL (Spring Data Jpa) How to set a database to all repositories except one which I'd like to set other database
SQL

(Spring Data Jpa) How to set a database to all repositories except one which I'd like to set other database


I have a large project, and I inserted a new entity with a Jpa Repository, I’d like this specific entity use a specific database unlike the one I was using for the others repositories, setting basePackages to primary database is not an option because the main repositories are randomly splitted along 10k of packages in all project.

Below is what I tried:

Main database:

package org.crm.infra.dbConfig;

import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
import org.crm.domain.entidadesBusiness.assinantesSistema.AssinanteRepository;
import org.springframework.context.annotation.*;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.jdbc.datasource.DriverManagerDataSource;

import javax.sql.DataSource;

@Configuration
public class DataSourceConfig {

    @Bean
    @Primary
    public DataSource dataSource() {
        HikariConfig hikariConfig = new HikariConfig();
        hikariConfig.setJdbcUrl("secret");
        hikariConfig.setUsername("secret");
        hikariConfig.setPassword("secret");

        // Configurações do HikariCP
        hikariConfig.setConnectionTestQuery("SELECT 1");
        hikariConfig.setValidationTimeout(3000);
        hikariConfig.setIdleTimeout(60000);
        hikariConfig.setMaxLifetime(1800000);
        hikariConfig.setMinimumIdle(5);
        hikariConfig.setMaximumPoolSize(20);

        return new HikariDataSource(hikariConfig);
    }
}

The secondary database (for new entity):

package org.crm.infra.dbConfig;

import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
import jakarta.persistence.EntityManagerFactory;
import org.crm.domain.entidadesBusiness.assinantesSistema.Assinante;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;

import javax.sql.DataSource;

@Configuration
@EnableJpaRepositories(
        basePackages = "org.crm.domain.entidadesBusiness.assinantesSistema", // Pacote onde está o repositório do Assinante
        entityManagerFactoryRef = "assinanteEntityManagerFactory",
        transactionManagerRef = "assinanteTransactionManager"
)
public class AssinanteDataSourceConfig {

    @Bean
    public DataSource assinanteDataSource() {
        HikariConfig hikariConfig = new HikariConfig();
        hikariConfig.setJdbcUrl("secret");
        hikariConfig.setUsername("secret");
        hikariConfig.setPassword("secret");

        // Configurações do HikariCP
        hikariConfig.setConnectionTestQuery("SELECT 1");
        hikariConfig.setValidationTimeout(3000);
        hikariConfig.setIdleTimeout(60000);
        hikariConfig.setMaxLifetime(1800000);
        hikariConfig.setMinimumIdle(5);
        hikariConfig.setMaximumPoolSize(20);

        return new HikariDataSource(hikariConfig);
    }

    @Bean
    public LocalContainerEntityManagerFactoryBean assinanteEntityManagerFactory() {
        LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean();
        factory.setDataSource(assinanteDataSource());
        factory.setPackagesToScan(Assinante.class.getPackageName());
        factory.setJpaVendorAdapter(new HibernateJpaVendorAdapter());
        // Configure outras propriedades do Hibernate se necessário
        return factory;
    }

    @Bean
    public JpaTransactionManager assinanteTransactionManager(EntityManagerFactory assinanteEntityManagerFactory) {
        return new JpaTransactionManager(assinanteEntityManagerFactory);
    }
}

The problem is when I start my app I get:

Consider defining a bean of type 'org.crm.infra.entidades.user.UserRepository' in your configuration.

Which is a entity managed for the primary database, it sounds like spring boot is going AssinanteDataSourceConfig to all entities



You need to sign in to view this answers

Exit mobile version