admin管理员组文章数量:1794759
springboot jpa 配置多数据源报错解决 Consider defining a bean named ‘entityManagerFactory‘
版本
springboot 2.6.x
现象jpa配置多数据源后启动报错
Consider defining a bean named ‘entityManagerFactory’
解决方法1:将其中一个datasource添加@Primary注解 方法2:自行定义entityManagerFactory
private static JpaVendorAdapter jpaVendorAdapter(JpaProperties properties) { AbstractJpaVendorAdapter adapter = new HibernateJpaVendorAdapter(); adapter.setShowSql(properties.isShowSql()); if (properties.getDatabase() != null) { adapter.setDatabase(properties.getDatabase()); } if (properties.getDatabasePlatform() != null) { adapter.setDatabasePlatform(properties.getDatabasePlatform()); } adapter.setGenerateDdl(properties.isGenerateDdl()); return adapter; } @Bean public EntityManagerFactoryBuilder entityManagerFactoryBuilder( ObjectProvider<PersistenceUnitManager> persistenceUnitManager, ObjectProvider<EntityManagerFactoryBuilderCustomizer> customizers ) { EntityManagerFactoryBuilder builder = new EntityManagerFactoryBuilder( jpaVendorAdapter(jpaProperties()), this.jpaProperties().getProperties(), (PersistenceUnitManager) persistenceUnitManager.getIfAvailable() ); customizers.orderedStream().forEach((customizer) -> { customizer.customize(builder); }); return builder; } 源码分析springboot jpa自动配置需要存在单一候选DataSource时才会生效 org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaConfiguration
@Configuration(proxyBeanMethods = false) @EnableConfigurationProperties(HibernateProperties.class) @ConditionalOnSingleCandidate(DataSource.class) class HibernateJpaConfiguration extends JpaBaseConfiguration {...}本文标签: 报错jpaSpringBootdefiningentityManagerFactory
版权声明:本文标题:springboot jpa 配置多数据源报错解决 Consider defining a bean named ‘entityManagerFactory‘ 内容由林淑君副主任自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.xiehuijuan.com/baike/1686558839a81791.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论