Could not autowire. Qualified bean must be of 'EntityManagerFactory' type

435 Views Asked by At

I upgraded Java from 1.8 to 17 , Spring Boot from 2.1.18.RELEASE to 3.1.1 and Postgresql from 42.2.16.jre7 to 42.6.0

I was using this in my app prop: spring.jpa.database-platform = org.hibernate.dialect.PostgreSQL94Dialect

I changed it with this: spring.jpa.database-platform = org.hibernate.dialect.PostgreSQLDialect

I am using multiple database connections; primary and second. The code below was working fine before the upgrade.

import org.springframework.aop.Advisor;
import org.springframework.aop.aspectj.AspectJExpressionPointcut;
import org.springframework.aop.support.DefaultPointcutAdvisor;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.data.domain.AuditorAware;
import org.springframework.data.jpa.repository.config.EnableJpaAuditing;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.jdbc.datasource.SimpleDriverDataSource;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import jakarta.persistence.EntityManagerFactory;
import javax.sql.DataSource;
import java.sql.DriverManager;
import java.sql.SQLException;

@Configuration
@EnableJpaAuditing
@EnableTransactionManagement
@EnableJpaRepositories(
entityManagerFactoryRef = "primaryEntityManagerFactory",
transactionManagerRef = "primaryTransactionManager",
basePackages = {"xxxxx"})
public class PrimaryDatabaseConfig {

    @Primary
    @Bean(name = "primaryDataSourceConfig")
    @ConfigurationProperties("spring.datasource")
    public DataSourceProperties dataSourceProperties() {
        return new DataSourceProperties();
    }
    
    @Primary
    @Bean(name = "primaryDataSource")
    public DataSource dataSource(@Qualifier("primaryDataSourceConfig") DataSourceProperties dataSourceProperties) throws SQLException {
        String url = dataSourceProperties.determineUrl();
        String user = dataSourceProperties.determineUsername();
        String password = dataSourceProperties.determinePassword();
        return new SimpleDriverDataSource(DriverManager.getDriver(url), url, user, password);
    }
    
    @Primary
    @Bean(name = "primaryEntityManagerFactory")
    public LocalContainerEntityManagerFactoryBean primaryEntityManagerFactory(EntityManagerFactoryBuilder builder, @Qualifier("primaryDataSource") DataSource dataSource) {
        return builder.dataSource(dataSource)
                .packages("xxxxx")
                .persistenceUnit("primaryPU")
                .build();
    }
    
    @Primary
    @Bean(name = "primaryTransactionManager")
    public PlatformTransactionManager primaryTransactionManager(@Qualifier("primaryEntityManagerFactory") EntityManagerFactory entityManagerFactory) {
        return new JpaTransactionManager(entityManagerFactory);
    }

}

I was importing EntityManagerFactory from javax.

"import javax.persistence.EntityManagerFactory;"

But with JDK17 I read that jakarta should be used instead of javax and in my project i replaced javax with jakarta. It works fine in other places I changed. But after the change here I am getting the following error.

enter image description here

What I've tried:

I downgraded the Postgres version and reverted the changes. It caused conflict.

  1. I added the following dependency to my pom file:
\<dependency\>  
\<groupId\>javax.persistence\</groupId\>  
\<artifactId\>javax.persistence-api\</artifactId\>  
\<version\>2.2\</version\>
\</dependency\>

But it need to jakarta, not javax. public JpaTransactionManager( jakarta.persistence.EntityManagerFactory emf )

  1. I changed my application.properties from this :

spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl spring.jpa.properties.hibernate.implicit_naming_strategy=org.springframework.boot.orm.jpa.hibernate.SpringImplicitNamingStrategy spring.jpa.properties.hibernate.physical_naming_strategy=org.springframework.boot.orm.jpa.hibernate.SpringPhysicalNamingStrategy

to this: spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.CamelCaseToUnderscoresNamingStrategy spring.jpa.hibernate.naming.implicit-strategy=org.springframework.boot.orm.jpa.hibernate.SpringImplicitNamingStrategy

I couldn't find any other solution about this error.

How can i fix it?

0

There are 0 best solutions below