헤르메스 LIFE

[Exception] Multi Database 시 Bean 클래스 Repository Call 오류 본문

Exception

[Exception] Multi Database 시 Bean 클래스 Repository Call 오류

헤르메스의날개 2022. 2. 21. 00:33
728x90

개발환경

1. STS 버전 : 4.13.1

2. JDK 버전 : OpenJDK 11.0.14_9_x64

3. Tomcat 버전 : 9.0.56

4. Maven 버전 : 3.8.4

5. Spring 버전 : Spring Boot 2.6.3

6. Database : Docker 에 DB 설치

- primary - PostgreSQL 13.3
- secondary - MySQL DB 8.0.28

8. lombok

https://hermeslog.tistory.com/568

 

[Spring Boot] HikariCP를 이용한 Multi Database Connection + JPA

개발환경 1. STS 버전 : 4.13.1 2. JDK 버전 : OpenJDK 11.0.14_9_x64 3. Tomcat 버전 : 9.0.56 4. Maven 버전 : 3.8.4 5. Spring 버전 : Spring Boot 2.6.3 6. Database : Docker 에 DB 설치 - primary - Postgr..

hermeslog.tistory.com


PrimaryDatasourceConfig.java

@Configuration
@EnableJpaRepositories(basePackages = { "com.study.springboot" },    // Repository 경로
        entityManagerFactoryRef = "primeEntityManager", transactionManagerRef = "primeTransactionManager")
public class PrimaryDatasourceConfig {
...
}

SecondaryDatasourceConfiguration.java

@Configuration
@EnableJpaRepositories(basePackages = { "com.study.springboot" },     // Repository 경로
        entityManagerFactoryRef = "productEntityManager", transactionManagerRef = "productTransactionManager")
public class SecondaryDatasourceConfiguration {
...
}

 

문제는 @EnableJpaRepositories(basePackages = { "com.study.springboot" } 이 부분입니다.

Repository 경로가 중복되어 있어서, 각 DB 설정에 Repository 가 잘못 매칭되어 발생한 오류 입니다.

아래와 같이 변경했더니 해결되었습니다.

PrimaryDatasourceConfig.java

@Configuration
@EnableJpaRepositories(basePackages = { "com.study.springboot.system" },    // Repository 경로
        entityManagerFactoryRef = "primeEntityManager", transactionManagerRef = "primeTransactionManager")
public class PrimaryDatasourceConfig {
...
}

SecondaryDatasourceConfiguration.java

@Configuration
@EnableJpaRepositories(basePackages = { "com.study.springboot.product" },     // Repository 경로
        entityManagerFactoryRef = "productEntityManager", transactionManagerRef = "productTransactionManager")
public class SecondaryDatasourceConfiguration {
...
}

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2022-02-21 00:20:58.019 ERROR 20860 --- [           main] o.s.boot.SpringApplication               : Application run failed

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'memberRepository' defined in com.study.springboot.system.repository.MemberRepository defined in @EnableJpaRepositories declared on SecondaryDatasourceConfiguration: Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Not a managed type: class com.study.springboot.system.entity.Member
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1804)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:620)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:542)
	at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:335)
	at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234)
	at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:333)
	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:208)
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:934)
	at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:918)
	at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:583)
	at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:145)
	at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:732)
	at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:414)
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:302)
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:1303)
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:1292)
	at com.study.springboot.SpringbootApplication.main(SpringbootApplication.java:14)
Caused by: java.lang.IllegalArgumentException: Not a managed type: class com.study.springboot.system.entity.Member
	at org.hibernate.metamodel.internal.MetamodelImpl.managedType(MetamodelImpl.java:582)
	at org.hibernate.metamodel.internal.MetamodelImpl.managedType(MetamodelImpl.java:85)
	at org.springframework.data.jpa.repository.support.JpaMetamodelEntityInformation.<init>(JpaMetamodelEntityInformation.java:75)
	at org.springframework.data.jpa.repository.support.JpaEntityInformationSupport.getEntityInformation(JpaEntityInformationSupport.java:66)
	at org.springframework.data.jpa.repository.support.JpaRepositoryFactory.getEntityInformation(JpaRepositoryFactory.java:232)
	at org.springframework.data.jpa.repository.support.JpaRepositoryFactory.getTargetRepository(JpaRepositoryFactory.java:181)
	at org.springframework.data.jpa.repository.support.JpaRepositoryFactory.getTargetRepository(JpaRepositoryFactory.java:164)
	at org.springframework.data.jpa.repository.support.JpaRepositoryFactory.getTargetRepository(JpaRepositoryFactory.java:75)
	at org.springframework.data.repository.core.support.RepositoryFactorySupport.getRepository(RepositoryFactorySupport.java:324)
	at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.lambda$afterPropertiesSet$5(RepositoryFactoryBeanSupport.java:322)
	at org.springframework.data.util.Lazy.getNullable(Lazy.java:230)
	at org.springframework.data.util.Lazy.get(Lazy.java:114)
	at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.afterPropertiesSet(RepositoryFactoryBeanSupport.java:328)
	at org.springframework.data.jpa.repository.support.JpaRepositoryFactoryBean.afterPropertiesSet(JpaRepositoryFactoryBean.java:144)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1863)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1800)
	... 16 common frames omitted

 

 

 

728x90