Spring Data Jpa 创建名为“entityManagerFactory”的Bean时出错工厂方法“entityManagerFactory”引发异常并显示消息

6psbrbz9  于 8个月前  发布在  Spring
关注(0)|答案(1)|浏览(136)

我有一个webapp,里面有maven,eclipselink,jpa,spring 5,jsf2.3,java 8,primefaces 11,部署在tomcat8.5上。
对于一个软件现代化的问题,我刚刚成功地移植到tomcat 9,java 17保留了spring 5和所有其余的库。
但这只是移植到tomcat10.0、java 17和所有依赖项升级到jakarta(包括升级到spring 6)的中间步骤:这个端口有几个问题。
我被一个实体管理器配置的问题困扰了一段时间。
应用程序是从一个扩展org.springframework.web.servlet.support.AbstractDispatcherServletInitializer的类配置的,在onStartup方法中,我定义了FacesServlet和其他jsf/primefaces东西。在getRootConfigClasses方法中,我列出了我的各种spring配置文件:

@Override
protected Class<?>[] getRootConfigClasses() {
     return new Class[] { 
             WebMvcConfig.class,
             RepositoryConfig.class,
             SecurityConfiguration.class,
             CachingConfig.class,
             MethodSecurityConfig.class,
             HttpClientConfig.class
             }; 
}

问题是RepositoryConfig,这是类:

@Configuration
@EnableTransactionManagement
@EnableJpaRepositories("here.my.package.with.repositories")
public class RepositoryConfig implements TransactionManagementConfigurer {
    
    protected Logger logger = LogManager.getLogger(this.getClass());
    
    @Autowired 
    @Qualifier("config")
    private Config config;  
    
    @Bean(name="entityManagerFactory")
    public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
        LocalContainerEntityManagerFactoryBean emf = new LocalContainerEntityManagerFactoryBean();
        
        emf.setDataSource(dataSource());
        
        emf.setPackagesToScan(new String[] { 
                "here.my.package.with.entities"
                });
        
        emf.setJpaVendorAdapter(jpaAdapter());
        
        Properties jpaProperties = new Properties();
        
        JpaDialect jpaDialect = new EclipseLinkJpaDialect();
        emf.setJpaDialect(jpaDialect);
        
        jpaProperties.put("eclipselink.weaving", "false");
        
        
        emf.setJpaProperties(jpaProperties);

        return emf;
    }
    
    
    @Bean(name="jpaAdapter")
    public EclipseLinkJpaVendorAdapter jpaAdapter() {
        EclipseLinkJpaVendorAdapter hjva = new EclipseLinkJpaVendorAdapter();
        hjva.setDatabasePlatform("org.eclipse.persistence.platform.database.MySQLPlatform");
        hjva.setGenerateDdl(false);
        hjva.setShowSql(false);
        return hjva;
    }
    
    @Bean(name="dataSource")
    public DataSource dataSource() {
        JndiTemplate template = new JndiTemplate();
        DataSource ds = null;
        try {
            ds = (DataSource) template.lookup("java:/comp/env/" + config.get("repository.jndi.name"));
        } catch (NamingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return ds;
    }

    
    @Bean(name="transactionManager")
    public PlatformTransactionManager transactionManager() {
        JpaTransactionManager jpaTransactionManager = new JpaTransactionManager();
        try {
            jpaTransactionManager.setEntityManagerFactory(entityManagerFactory().getObject());
            jpaTransactionManager.setDataSource(dataSource());
        } catch (Exception e) {
            logger.error(e.getMessage());
        }
        return jpaTransactionManager;
    } 
    
    @Override
    public PlatformTransactionManager annotationDrivenTransactionManager() {
        return transactionManager();
    }   
    
    @Bean(name="persistenceAnnotation")
    public PersistenceAnnotationBeanPostProcessor persistenceAnnotation() {
        return new PersistenceAnnotationBeanPostProcessor();
    }
}

我得到这个错误:

Error creating bean with name 'entityManagerFactory' defined in it.baseapp.conf.RepositoryConfig: Failed to instantiate [org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean]: Factory method 'entityManagerFactory' threw exception with message: Error creating bean with name 'entityManagerFactory' defined in it.baseapp.conf.RepositoryConfig: Failed to instantiate
..
Error creating bean with name 'entityManagerFactory' defined in it.baseapp.conf.RepositoryConfig: Unexpected exception during bean creation
..
it always repeats this error for several lines
...
12:47:54.521 [main] ERROR it.baseapp.conf.RepositoryConfig$$SpringCGLIB$$0 - @Bean method RepositoryConfig.entityManagerFactory called as bean reference for type [org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean] but overridden by non-compatible bean instance of type [jdk.proxy3.$Proxy76]. Overriding bean of same name declared in: it.baseapp.conf.RepositoryConfig
[EL Info]: 2023-07-05 12:47:55.433--ServerSession(2050864629)--EclipseLink, version: Eclipse Persistence Services - 4.0.2.v202306161219
[EL Warning]: metamodel: 2023-07-05 12:47:56.449--The collection of metamodel types is empty. Model classes may not have been found during entity search for Java SE and some Java EE container managed persistence units.  Please verify that your entity classes are referenced in persistence.xml using either <class> elements or a global <exclude-unlisted-classes>false</exclude-unlisted-classes> element
[EL Warning]: metamodel: 2023-07-05 12:47:57.378--The collection of metamodel [ManagedType] types is empty. Model classes may not have been found during entity search for Java SE and some Java EE container managed persistence units.  Please verify that your entity classes are referenced in persistence.xml using either <class> elements or a global <exclude-unlisted-classes>false</exclude-unlisted-classes> element.  The lookup on [class here.my.package.with.entities.OneOfMyEntities] will return null.
[EL Warning]: metamodel: 2023-07-05 12:47:57.618--The collection of metamodel [ManagedType] types is empty. Model classes may not have been found during entity search for Java SE and some Java EE container managed persistence units.  Please verify that your entity classes are referenced in persistence.xml using either <class> elements or a global <exclude-unlisted-classes>false</exclude-unlisted-classes> element.  The lookup on [class here.my.package.with.entities.OneOfMyEntities] will return null.
12:47:57.637 [main] ERROR org.springframework.web.context.ContextLoader - Context initialization failed

我从pom.xml中摘录了一段关于相关依赖的内容:

<dependency>   
    <groupId>org.eclipse.persistence</groupId>
    <artifactId>eclipselink</artifactId>
    <version>4.0.2</version>
</dependency>

<dependency>
    <groupId>org.springframework.data</groupId>
    <artifactId>spring-data-jpa</artifactId>
    <version>3.1.1</version>
</dependency>

various spring dependency at version 6.0.10
for example:
 <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-core</artifactId>
    <version>6.0.10</version>
</dependency>

同一个类“RepositoryConfig”在本文开头指出的旧版本的webapp中没有问题。有人遇到过类似的问题吗?

nx7onnlm

nx7onnlm1#

小更新,在我解决这部分错误的同时:

"Error creating bean with name 'entityManagerFactory'..@Bean method RepositoryConfig.entityManagerFactory called as bean reference for type [org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean] but overridden by non-compatible bean instance of type [jdk.proxy3.$Proxy76]. Overriding bean of same name declared in: it.baseapp.conf.RepositoryConfig"

我显式地添加了通过@DependsOn注解声明的bean之间的构建依赖关系:

@Bean(name="entityManagerFactory")
@DependsOn({"dataSource","jpaAdapter"})
public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
...
@Bean(name="transactionManager")
@DependsOn({"entityManagerFactory","dataSource"})
public PlatformTransactionManager transactionManager() {

奇怪的是,同一个Web应用程序的前一个版本中的同一个配置类在没有DependsOn的情况下工作。
此错误仍然存在:

[EL Warning]: metamodel: 2023-07-07 13:44:35.492--The collection of metamodel types is empty. Model classes may not have been found during entity search for Java SE and some Java EE container managed persistence units.  Please verify that your entity classes are referenced in persistence.xml using either <class> elements or a global <exclude-unlisted-classes>false</exclude-unlisted-classes> element

我会尽快更新这个答案,我也修复了这一部分。

相关问题