Search code examples
javaspring-bootspring-data-jpa

Spring Boot 3 - Multiple Data Sources - EntityManagerFactoryBuilder builder Compile Error


I need to use multiple data sources in my Spring Boot 3 application. I have configured all the connection properties correctly in my application.properties file (I have used the configuration for single data source projects in the past).

When I try to setup my @Primary data source (it's the only one I'm trying to setup at this time) - I'm getting a complaint at the line: return builder.dataSource(qaDataSource)

The error is:

cannot find symbol
  symbol:  method dataSource(DataSource)
  location: variable builder of type EntityManagerFactoryBuilder

Here's the class code:

import jakarta.persistence.EntityManagerFactory;
import javax.sql.DataSource;
import org.hibernate.jpa.boot.spi.EntityManagerFactoryBuilder;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

/**
 *
 * Configuration for the QA Datasource
 * 
 * 
 */
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
        entityManagerFactoryRef = "qaEntityManagerFactory",
        transactionManagerRef = "qaTransactionManager",
        basePackages = { "com.app.repositories" })
public class QADataConfiguration {

    @Primary
    @Bean(name="qaProperties")
    @ConfigurationProperties("spring.datasource")
    public DataSourceProperties dataSourceProperties() {

        return new DataSourceProperties();
    }
    
    @Primary
    @Bean(name="qaDatasource")
    @ConfigurationProperties(prefix = "spring.datasource")
    public DataSource datasource(@Qualifier("qaProperties") DataSourceProperties properties){

        return properties.initializeDataSourceBuilder().build();
    }

    @Primary
    @Bean(name="qaEntityManagerFactory")
    public LocalContainerEntityManagerFactoryBean entityManagerFactoryBean
            (EntityManagerFactoryBuilder builder,
             @Qualifier("qaDatasource") DataSource qaDataSource){

        return builder.dataSource(qaDataSource)
                .packages("com.app.config.data.entity.NotifConfig")
                .persistenceUnit("qa").build();
    }

    @Primary
    @Bean(name = "qaTransactionManager")
    @ConfigurationProperties("spring.jpa")
    public PlatformTransactionManager transactionManager(
            @Qualifier("userEntityManagerFactory") EntityManagerFactory entityManagerFactory) {

        return new JpaTransactionManager(entityManagerFactory);
    }

}

Compile Error:

-------------------------------------------------------------
COMPILATION ERROR : 
-------------------------------------------------------------
com/app/config/data/config/QADataConfiguration.java:[55,23] cannot find symbol
  symbol:   method dataSource(javax.sql.DataSource)
  location: variable builder of type org.hibernate.jpa.boot.spi.EntityManagerFactoryBuilder
1 error
-------------------------------------------------------------
------------------------------------------------------------------------
BUILD FAILURE
------------------------------------------------------------------------
Total time:  19.071 s
Finished at: 2023-07-25T08:02:25-07:00
------------------------------------------------------------------------
Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.11.0:compile (default-compile) on project sjconfigurator: Compilation failure
com/app/config/data/config/QADataConfiguration.java:[55,23] cannot find symbol
  symbol:   method dataSource(javax.sql.DataSource)
  location: variable builder of type org.hibernate.jpa.boot.spi.EntityManagerFactoryBuilder

I'm sure it's just a case of "stupid user detected" :) but if someone can help me out - I'd really appreciate it.


Solution

  • The answer was simple - and Mar-Z came up with the correct answer:

    Just a wrong class imported. Should be:

    import org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder; 
    

    and NOT:

    import org.hibernate.jpa.boot.spi.EntityManagerFactoryBuilder;