gpt4 book ai didi

spring - 使用来自不同数据库的 bean hibernate DuplicateMappingException

转载 作者:行者123 更新时间:2023-11-28 22:36:27 27 4
gpt4 key购买 nike

编辑:我今天早上意识到异常似乎与 Spring 启动执行器有关,它似乎只在生产中运行,而不是在从 spring 工具套件启动项目时运行。 (查看帖子末尾的异常跟踪)

我有一个 spring boot 项目,我最近从 1.5 版本升级到 2.2。

在这个项目中,我们从两个不同的数据库访问 bean。我按照 this article 中的步骤操作配置数据库。

使用 jpa 访问主数据库,有关此数据库的所有内容都在特定包中。

另一个数据库用于直接使用hibernate访问遗留bean。这些遗留 bean 的所有内容都在不同的包中。

有些bean在两个包中有相同的名字,但是由于它们是在两个不同的仓库中配置的,我认为这不会有问题。

当我在 Spring Tools Suite 中运行项目时(使用嵌入式 tomcat)一切正常。

但是如果我构建一个 war 并将其部署到一个独立的 tomcat(尝试使用版本 8.5 和 9),我会得到一个异常:

org.hibernate.DuplicateMappingException:[fr.mycompany.dbconfprimary.MyBean] 和 [fr.mycomany.dbconfsecondary.MyBean] 实体共享相同的 JPA 实体名称:[MyBean],这是不允许的!

我不明白为什么我在 war 部署上有这个问题,但在 spring boot 嵌入式 tomcat 上却没有。

这是主数据库的配置类:

    package fr.mycompany.dbconfprimary;

...
...

@Configuration
@EnableJpaRepositories(
basePackages = "fr.mycompany.dbconfprimary",
entityManagerFactoryRef = "entityManagerFactory",
transactionManagerRef = "transactionManager",
repositoryBaseClass = BaseRepositoryImpl.class)
public class HibernatePrimaryConfig {
...
...

@Bean
@Primary
public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
LocalContainerEntityManagerFactoryBean em
= new LocalContainerEntityManagerFactoryBean();
em.setDataSource(dataSource());
em.setPackagesToScan(
new String[] { "fr.mycompany.dbconfprimary" });

HibernateJpaVendorAdapter vendorAdapter
= new HibernateJpaVendorAdapter();
em.setJpaVendorAdapter(vendorAdapter);
HashMap<String, Object> properties = new HashMap<>();

properties.put("hibernate.hbm2ddl.auto",ddlMode);
properties.put("hibernate.dialect",dialect);
em.setJpaPropertyMap(properties);

return em;
}

@Primary
@Bean
public PlatformTransactionManager transactionManager() {

JpaTransactionManager transactionManager
= new JpaTransactionManager();
transactionManager.setEntityManagerFactory(
entityManagerFactory().getObject());
return transactionManager;
}

@Primary
@Bean("serversDataSource")
public DataSource dataSource() {

DriverManagerDataSource dataSource
= new DriverManagerDataSource();
dataSource.setDriverClassName(driverClassName);
dataSource.setUrl(url);
dataSource.setUsername(username);
dataSource.setPassword(password);

return dataSource;
}

}

这是辅助数据库的配置:

    package fr.mycompany.dbconfsecondary;
...
...

@Configuration
@EnableJpaRepositories(
basePackages = {"fr.mycompany.dbconfsecondary"},
entityManagerFactoryRef = "secondaryEntityManagerFactory",
transactionManagerRef = "secondaryTransactionManager")
public class HibernateSecondaryConfig {
...
...

@Bean
public LocalContainerEntityManagerFactoryBean secondaryEntityManagerFactory() {
LocalContainerEntityManagerFactoryBean em
= new LocalContainerEntityManagerFactoryBean();
em.setDataSource(secondaryDataSource());
em.setPackagesToScan(
new String[] {"fr.mycompany.dbconfsecondary");

String[] hbmFiles=new String[hibernateHbmResources.length];
for(int i=0;i<hibernateHbmResources.length;i++)
try {
hbmFiles[i]=convertHbmPath(hibernateHbmResources[i].getFile().getAbsolutePath());
} catch (IOException e) {
throw(new RuntimeException(e));
}
em.setMappingResources(hbmFiles);

HibernateJpaVendorAdapter vendorAdapter
= new HibernateJpaVendorAdapter();
em.setJpaVendorAdapter(vendorAdapter);
HashMap<String, Object> properties = new HashMap<>();

properties.put("hibernate.hbm2ddl.auto",ddlMode);
properties.put("hibernate.dialect",dialect);
properties.put("hibernate.current_session_context_class","thread");

em.setJpaPropertyMap(properties);

return em;
}

@Bean
public PlatformTransactionManager secondaryTransactionManager() {

JpaTransactionManager transactionManager
= new JpaTransactionManager();
transactionManager.setEntityManagerFactory(
secondaryEntityManagerFactory().getObject());
return transactionManager;
}

@Bean("secondaryServersDataSource")
public DataSource secondaryDataSource() {

DriverManagerDataSource dataSource
= new DriverManagerDataSource();
dataSource.setDriverClassName(driverClassName);
dataSource.setUrl(url);
dataSource.setUsername(username);
dataSource.setPassword(password);

return dataSource;
}
...
...

}

[几乎]完整的异常跟踪:

2019-12-11 10:08:39 [main] ERROR
o.s.b.w.e.tomcat.TomcatStarter - Error starting Tomcat context. Exception: org.springframework.beans.factory.BeanCreationException. Message: Error creating bean with name 'servletEndpointRegistrar' defined in class path resource [org/springframework/boot/actuate/autoconfigure/endpoint/web/ServletEndpointManagementContextConfiguration$WebMvcServletEndpointManagementContextConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.boot.actuate.endpoint.web.ServletEndpointRegistrar]: Factory method 'servletEndpointRegistrar' threw exception; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'healthEndpoint' defined in class path resource [org/springframework/boot/actuate/autoconfigure/health/HealthEndpointConfiguration.class]: Unsatisfied dependency expressed through method 'healthEndpoint' parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'healthContributorRegistry' defined in class path resource [org/springframework/boot/actuate/autoconfigure/health/HealthEndpointConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.boot.actuate.health.HealthContributorRegistry]: Factory method 'healthContributorRegistry' threw exception; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'applicationDiskUsage': Unsatisfied dependency expressed through field 'parameterService'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'parameterService': Unsatisfied dependency expressed through field 'paramEnumerationRepo'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'parametrageTypeEnumerationRepo': Cannot create inner bean '(inner bean)#255eadf5' of type [org.springframework.orm.jpa.SharedEntityManagerCreator] while setting bean property 'entityManager'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name '(inner bean)#255eadf5': Cannot resolve reference to bean 'entityManagerFactory' while setting constructor argument; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [fr/mycompany/ipolice/server/Hibernatev3Config.class]: Invocation of init method failed; nested exception is org.hibernate.DuplicateMappingException: The [fr.mycompany.secondarydb.BeanName] and [fr.mycompany.primarydb.BeanName] entities share the same JPA entity name: [BeanName] which is not allowed!
2019-12-11 10:08:39 [main] INFO
o.a.catalina.core.StandardService - Stopping service [Tomcat]
2019-12-11 10:08:39 [main] WARN
o.s.b.w.s.c.AnnotationConfigServletWebServerApplicationContext - Exception encountered during context initialization - cancelling refresh attempt: org.springframework.context.ApplicationContextException: Unable to start web server; nested exception is org.springframework.boot.web.server.WebServerException: Unable to start embedded Tomcat
2019-12-11 10:08:39 [main] INFO
o.s.b.a.l.ConditionEvaluationReportLoggingListener -
Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2019-12-11 10:08:39 [main] ERROR
o.s.boot.SpringApplication - Application run failed
org.springframework.context.ApplicationContextException: Unable to start web server; nested exception is org.springframework.boot.web.server.WebServerException: Unable to start embedded Tomcat
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.onRefresh(ServletWebServerApplicationContext.java:156)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:544)
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:141)
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:747)
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:315)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1226)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1215)
at fr.mycompany.Application.main(Application.java:41)
Caused by: org.springframework.boot.web.server.WebServerException: Unable to start embedded Tomcat
at org.springframework.boot.web.embedded.tomcat.TomcatWebServer.initialize(TomcatWebServer.java:126)
at org.springframework.boot.web.embedded.tomcat.TomcatWebServer.<init>(TomcatWebServer.java:88)
at org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory.getTomcatWebServer(TomcatServletWebServerFactory.java:438)
at org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory.getWebServer(TomcatServletWebServerFactory.java:191)
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.createWebServer(ServletWebServerApplicationContext.java:180)
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.onRefresh(ServletWebServerApplicationContext.java:153)
... 8 common frames omitted
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'servletEndpointRegistrar' defined in class path resource [org/springframework/boot/actuate/autoconfigure/endpoint/web/ServletEndpointManagementContextConfiguration$WebMvcServletEndpointManagementContextConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.boot.actuate.endpoint.web.ServletEndpointRegistrar]: Factory method 'servletEndpointRegistrar' threw exception; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'healthEndpoint' defined in class path resource [org/springframework/boot/actuate/autoconfigure/health/HealthEndpointConfiguration.class]: Unsatisfied dependency expressed through method 'healthEndpoint' parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'healthContributorRegistry' defined in class path resource [org/springframework/boot/actuate/autoconfigure/health/HealthEndpointConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.boot.actuate.health.HealthContributorRegistry]: Factory method 'healthContributorRegistry' threw exception; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'applicationDiskUsage': Unsatisfied dependency expressed through field 'parameterService'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'parameterService': Unsatisfied dependency expressed through field 'paramEnumerationRepo'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'parametrageTypeEnumerationRepo': Cannot create inner bean '(inner bean)#255eadf5' of type [org.springframework.orm.jpa.SharedEntityManagerCreator] while setting bean property 'entityManager'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name '(inner bean)#255eadf5': Cannot resolve reference to bean 'entityManagerFactory' while setting constructor argument; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [fr/mycompany/ipolice/server/Hibernatev3Config.class]: Invocation of init method failed; nested exception is org.hibernate.DuplicateMappingException: The [fr.mycompany.secondarydb.BeanName] and [fr.mycompany.primarydb.BeanName] entities share the same JPA entity name: [BeanName] which is not allowed!
at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:656)
at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:636)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1338)
...
... 13 common frames omitted
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.boot.actuate.endpoint.web.ServletEndpointRegistrar]: Factory method 'servletEndpointRegistrar' threw exception; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'healthEndpoint' defined in class path resource [org/springframework/boot/actuate/autoconfigure/health/HealthEndpointConfiguration.class]: Unsatisfied dependency expressed through method 'healthEndpoint' parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'healthContributorRegistry' defined in class path resource [org/springframework/boot/actuate/autoconfigure/health/HealthEndpointConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.boot.actuate.health.HealthContributorRegistry]: Factory method 'healthContributorRegistry' threw exception; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'applicationDiskUsage': Unsatisfied dependency expressed through field 'parameterService'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'parameterService': Unsatisfied dependency expressed through field 'paramEnumerationRepo'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'parametrageTypeEnumerationRepo': Cannot create inner bean '(inner bean)#255eadf5' of type [org.springframework.orm.jpa.SharedEntityManagerCreator] while setting bean property 'entityManager'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name '(inner bean)#255eadf5': Cannot resolve reference to bean 'entityManagerFactory' while setting constructor argument; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [fr/mycompany/ipolice/server/Hibernatev3Config.class]: Invocation of init method failed; nested exception is org.hibernate.DuplicateMappingException: The [fr.mycompany.secondarydb.BeanName] and [fr.mycompany.primarydb.BeanName] entities share the same JPA entity name: [BeanName] which is not allowed!
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'healthEndpoint' defined in class path resource [org/springframework/boot/actuate/autoconfigure/health/HealthEndpointConfiguration.class]: Unsatisfied dependency expressed through method 'healthEndpoint' parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'healthContributorRegistry' defined in class path resource [org/springframework/boot/actuate/autoconfigure/health/HealthEndpointConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.boot.actuate.health.HealthContributorRegistry]: Factory method 'healthContributorRegistry' threw exception; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'applicationDiskUsage': Unsatisfied dependency expressed through field 'parameterService'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'parameterService': Unsatisfied dependency expressed through field 'paramEnumerationRepo'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'parametrageTypeEnumerationRepo': Cannot create inner bean '(inner bean)#255eadf5' of type [org.springframework.orm.jpa.SharedEntityManagerCreator] while setting bean property 'entityManager'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name '(inner bean)#255eadf5': Cannot resolve reference to bean 'entityManagerFactory' while setting constructor argument; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [fr/mycompany/ipolice/server/Hibernatev3Config.class]: Invocation of init method failed; nested exception is org.hibernate.DuplicateMappingException: The [fr.mycompany.secondarydb.BeanName] and [fr.mycompany.primarydb.BeanName] entities share the same JPA entity name: [BeanName] which is not allowed!
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'healthContributorRegistry' defined in class path resource [org/springframework/boot/actuate/autoconfigure/health/HealthEndpointConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.boot.actuate.health.HealthContributorRegistry]: Factory method 'healthContributorRegistry' threw exception; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'applicationDiskUsage': Unsatisfied dependency expressed through field 'parameterService'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'parameterService': Unsatisfied dependency expressed through field 'paramEnumerationRepo'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'parametrageTypeEnumerationRepo': Cannot create inner bean '(inner bean)#255eadf5' of type [org.springframework.orm.jpa.SharedEntityManagerCreator] while setting bean property 'entityManager'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name '(inner bean)#255eadf5': Cannot resolve reference to bean 'entityManagerFactory' while setting constructor argument; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [fr/mycompany/ipolice/server/Hibernatev3Config.class]: Invocation of init method failed; nested exception is org.hibernate.DuplicateMappingException: The [fr.mycompany.secondarydb.BeanName] and [fr.mycompany.primarydb.BeanName] entities share the same JPA entity name: [BeanName] which is not allowed!
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.boot.actuate.health.HealthContributorRegistry]: Factory method 'healthContributorRegistry' threw exception; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'applicationDiskUsage': Unsatisfied dependency expressed through field 'parameterService'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'parameterService': Unsatisfied dependency expressed through field 'paramEnumerationRepo'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'parametrageTypeEnumerationRepo': Cannot create inner bean '(inner bean)#255eadf5' of type [org.springframework.orm.jpa.SharedEntityManagerCreator] while setting bean property 'entityManager'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name '(inner bean)#255eadf5': Cannot resolve reference to bean 'entityManagerFactory' while setting constructor argument; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [fr/mycompany/ipolice/server/Hibernatev3Config.class]: Invocation of init method failed; nested exception is org.hibernate.DuplicateMappingException: The [fr.mycompany.secondarydb.BeanName] and [fr.mycompany.primarydb.BeanName] entities share the same JPA entity name: [BeanName] which is not allowed!
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'applicationDiskUsage': Unsatisfied dependency expressed through field 'parameterService'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'parameterService': Unsatisfied dependency expressed through field 'paramEnumerationRepo'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'parametrageTypeEnumerationRepo': Cannot create inner bean '(inner bean)#255eadf5' of type [org.springframework.orm.jpa.SharedEntityManagerCreator] while setting bean property 'entityManager'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name '(inner bean)#255eadf5': Cannot resolve reference to bean 'entityManagerFactory' while setting constructor argument; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [fr/mycompany/ipolice/server/Hibernatev3Config.class]: Invocation of init method failed; nested exception is org.hibernate.DuplicateMappingException: The [fr.mycompany.secondarydb.BeanName] and [fr.mycompany.primarydb.BeanName] entities share the same JPA entity name: [BeanName] which is not allowed!
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'parameterService': Unsatisfied dependency expressed through field 'paramEnumerationRepo'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'parametrageTypeEnumerationRepo': Cannot create inner bean '(inner bean)#255eadf5' of type [org.springframework.orm.jpa.SharedEntityManagerCreator] while setting bean property 'entityManager'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name '(inner bean)#255eadf5': Cannot resolve reference to bean 'entityManagerFactory' while setting constructor argument; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [fr/mycompany/ipolice/server/Hibernatev3Config.class]: Invocation of init method failed; nested exception is org.hibernate.DuplicateMappingException: The [fr.mycompany.secondarydb.BeanName] and [fr.mycompany.primarydb.BeanName] entities share the same JPA entity name: [BeanName] which is not allowed!
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'parametrageTypeEnumerationRepo': Cannot create inner bean '(inner bean)#255eadf5' of type [org.springframework.orm.jpa.SharedEntityManagerCreator] while setting bean property 'entityManager'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name '(inner bean)#255eadf5': Cannot resolve reference to bean 'entityManagerFactory' while setting constructor argument; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [fr/mycompany/ipolice/server/Hibernatev3Config.class]: Invocation of init method failed; nested exception is org.hibernate.DuplicateMappingException: The [fr.mycompany.secondarydb.BeanName] and [fr.mycompany.primarydb.BeanName] entities share the same JPA entity name: [BeanName] which is not allowed!
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name '(inner bean)#255eadf5': Cannot resolve reference to bean 'entityManagerFactory' while setting constructor argument; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [fr/mycompany/ipolice/server/Hibernatev3Config.class]: Invocation of init method failed; nested exception is org.hibernate.DuplicateMappingException: The [fr.mycompany.secondarydb.BeanName] and [fr.mycompany.primarydb.BeanName] entities share the same JPA entity name: [BeanName] which is not allowed!
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [fr/mycompany/ipolice/server/Hibernatev3Config.class]: Invocation of init method failed; nested exception is org.hibernate.DuplicateMappingException: The [fr.mycompany.secondarydb.BeanName] and [fr.mycompany.primarydb.BeanName] entities share the same JPA entity name: [BeanName] which is not allowed!
Caused by: org.hibernate.DuplicateMappingException: The [fr.mycompany.secondarydb.BeanName] and [fr.mycompany.primarydb.BeanName] entities share the same JPA entity name: [BeanName] which is not allowed!
at org.hibernate.boot.internal.InFlightMetadataCollectorImpl.addEntityBinding(InFlightMetadataCollectorImpl.java:305)
at org.hibernate.cfg.AnnotationBinder.bindClass(AnnotationBinder.java:822)
... 148 common frames omitted

最佳答案

问题是您应该直接禁用默认的 Spring Data JPA 存储库初始化。

这可以通过指定 spring.data.jpa.repositories.enabled=false 来实现配置属性(通过启动参数或通过 application.properties )

关于spring - 使用来自不同数据库的 bean hibernate DuplicateMappingException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59137905/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com