Home > Blockchain >  Qualified bean must be of 'EntityManagerFactory' type in Spring Boot 3. Works with Spring
Qualified bean must be of 'EntityManagerFactory' type in Spring Boot 3. Works with Spring

Time:12-21

I'm currently upgrading my project to Spring Boot 3 using a own JpaTransactionmanager configuration.

My code looks like following snippet:

@Configuration
@EnableJpaRepositories(
    entityManagerFactoryRef = "myEntityManagerFactory",
    transactionManagerRef = "myTransactionManager"
)
public class MyPersistenceConfig {

    ...

    @Bean
    public LocalContainerEntityManagerFactoryBean myEntityManagerFactory(DataSource dataSource) {
        var emfBean = new LocalContainerEntityManagerFactoryBean();

        // Some configuration removed

        return emfBean;
    }

    @Bean
    public JpaTransactionManager myTransactionManager(@Qualifier("myEntityManagerFactory") EntityManagerFactory emf) {
        var transactionManager = new JpaTransactionManager();
        transactionManager.setEntityManagerFactory(emf);
        return transactionManager;
    }
}

However, this code works in latest Spring Boot 2 but not with Spring Boot 3. Following (minimized) error is thrown on startup:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'myEntityManagerFactory' defined in class path resource [<path>/<to>/MyPersistenceConfig.class]: Unable to resolve name [org.springframework.boot.orm.jpa.hibernate.SpringPhysicalNamingStrategy] as strategy [org.hibernate.boot.model.naming.PhysicalNamingStrategy]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1751) ~[spring-beans-6.0.2.jar:6.0.2]
...
Caused by: org.hibernate.boot.registry.selector.spi.StrategySelectionException: Unable to resolve name [org.springframework.boot.orm.jpa.hibernate.SpringPhysicalNamingStrategy] as strategy [org.hibernate.boot.model.naming.PhysicalNamingStrategy]
...
Caused by: org.hibernate.boot.registry.classloading.spi.ClassLoadingException: Unable to load class [org.springframework.boot.orm.jpa.hibernate.SpringPhysicalNamingStrategy]
    at org.hibernate.boot.registry.classloading.internal.ClassLoaderServiceImpl.classForName(ClassLoaderServiceImpl.java:123) ~[hibernate-core-6.1.5.Final.jar:6.1.5.Final]
...
Caused by: java.lang.ClassNotFoundException: Could not load requested class : org.springframework.boot.orm.jpa.hibernate.SpringPhysicalNamingStrategy
    at org.hibernate.boot.registry.classloading.internal.AggregatedClassLoader.findClass(AggregatedClassLoader.java:210) ~[hibernate-core-6.1.5.Final.jar:6.1.5.Final]
...

Besides IntelliJ says on the JpaTransactionManager-Bean: Could not autowire. Qualified bean must be of 'EntityManagerFactory' type..

According the LocalContainerEntityManagerFactoryBean-JavaDoc, it should produce a EntityManagerFactory-Bean (and actually did with Spring-ORM 5):

FactoryBean that creates a JPA EntityManagerFactory according to JPA's standard container bootstrap contract. 

Is there anything that I'm missing?

CodePudding user response:

The problem seems to be the org.springframework.boot.orm.jpa.hibernate.SpringPhysicalNamingStrategy which has been deprecated since Spring Boot 2.6 and removed in Spring Boot 3 in favor of the CamelCaseToUnderscoresNamingStrategy. See here: https://docs.spring.io/spring-boot/docs/2.7.1/api/index.html?org/springframework/boot/orm/jpa/hibernate/SpringPhysicalNamingStrategy.html

  • Related