java 在Sping Boot 3中,限定的Bean必须为“EntityManagerFactory”类型,与Spring Boot 2一起使用

cgfeq70w  于 2022-12-21  发布在  Java
关注(0)|答案(1)|浏览(320)

我目前正在使用自己的JpaTransactionmanager配置将我的项目升级到Spring Boot 3。
我的代码看起来像下面的片段:

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

但是,此代码在最新的Spring Boot 2中有效,但在Spring Boot 3中无效。启动时抛出以下(最小化)错误:

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

此外,IntelliJ在JpaTransactionManager-Bean上说:Could not autowire. Qualified bean must be of 'EntityManagerFactory' type..
根据LocalContainerEntityManagerFactoryBean-JavaDoc,它应该生成一个EntityManagerFactory-Bean(实际上是用Spring-ORM 5生成的):

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

有什么我错过了吗?

6ie5vjzr

6ie5vjzr1#

问题似乎出在org.springframework.boot.orm.jpa.hibernate.SpringPhysicalNamingStrategy上,它从Sping Boot 2.6开始就被弃用,在Spring Boot 3中被删除,取而代之的是CamelCaseToUnderscoresNamingStrategyhttps://docs.spring.io/spring-boot/docs/2.7.1/api/index.html?org/springframework/boot/orm/jpa/hibernate/SpringPhysicalNamingStrategy.html

相关问题