빵상
2018-08-09 16:01:17 작성 2018-08-09 16:01:55 수정됨
8
6545

Spring boot 2.0 jpa 설정 질문입니다.


현재 mybatis와  hibernate 두개를 한 프로잭트에 동시에 사용을 하기위하여 셋팅중입니다.

mybatis 는 연동을 완료하였고 hibernate연동 중 오류가 발생하여 애를 먹고 있습니다.

스프링 버전 2.0.0 RELEASE, jdk 1.8 입니다.

2018-08-09 15:21:44.184  INFO 38558 --- [           main] org.hibernate.Version                    : HHH000412: Hibernate Core {5.2.10.Final}

2018-08-09 15:21:44.185  INFO 38558 --- [           main] org.hibernate.cfg.Environment            : HHH000206: hibernate.properties not found

2018-08-09 15:21:44.230  INFO 38558 --- [           main] o.hibernate.annotations.common.Version   : HCANN000001: Hibernate Commons Annotations {5.0.1.Final}

2018-08-09 15:21:44.419  INFO 38558 --- [           main] org.hibernate.dialect.Dialect            : HHH000400: Using dialect: org.hibernate.dialect.MariaDBDialect

2018-08-09 15:21:44.639  INFO 38558 --- [           main] j.LocalContainerEntityManagerFactoryBean : Initialized JPA EntityManagerFactory for persistence unit 'default'

2018-08-09 15:21:44.884  WARN 38558 --- [           main] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'qnaController': Unsatisfied dependency expressed through field 'qnaRepository'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'qnaRepository': Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Not a managed type: class com.intermind.kr.model.Qna

2018-08-09 15:21:44.884  INFO 38558 --- [           main] j.LocalContainerEntityManagerFactoryBean : Closing JPA EntityManagerFactory for persistence unit 'default'

2018-08-09 15:21:44.885  WARN 38558 --- [           main] o.s.b.f.support.DisposableBeanAdapter    : Invocation of destroy method 'close' failed on bean with name 'sqlSessionTemplate': java.lang.UnsupportedOperationException: Manual close is not allowed over a Spring managed SqlSession

2018-08-09 15:21:44.886  INFO 38558 --- [           main] o.apache.catalina.core.StandardService   : Stopping service [Tomcat]

2018-08-09 15:21:44.903  INFO 38558 --- [           main] ConditionEvaluationReportLoggingListener : 


Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.

2018-08-09 15:21:44.910 ERROR 38558 --- [           main] o.s.boot.SpringApplication               : Application run failed


org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'qnaController': Unsatisfied dependency expressed through field 'qnaRepository'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'qnaRepository': Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Not a managed type: class com.intermind.kr.model.Qna

at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:587) ~[spring-beans-5.0.4.RELEASE.jar:5.0.4.RELEASE]

at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:91) ~[spring-beans-5.0.4.RELEASE.jar:5.0.4.RELEASE]

at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:373) ~[spring-beans-5.0.4.RELEASE.jar:5.0.4.RELEASE]

at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1344) ~[spring-beans-5.0.4.RELEASE.jar:5.0.4.RELEASE]

at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:582) ~[spring-beans-5.0.4.RELEASE.jar:5.0.4.RELEASE]

at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:502) ~[spring-beans-5.0.4.RELEASE.jar:5.0.4.RELEASE]

at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:312) ~[spring-beans-5.0.4.RELEASE.jar:5.0.4.RELEASE]

at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228) ~[spring-beans-5.0.4.RELEASE.jar:5.0.4.RELEASE]

at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:310) ~[spring-beans-5.0.4.RELEASE.jar:5.0.4.RELEASE]

at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:200) ~[spring-beans-5.0.4.RELEASE.jar:5.0.4.RELEASE]

at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:760) ~[spring-beans-5.0.4.RELEASE.jar:5.0.4.RELEASE]

at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:868) ~[spring-context-5.0.4.RELEASE.jar:5.0.4.RELEASE]

at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:549) ~[spring-context-5.0.4.RELEASE.jar:5.0.4.RELEASE]

at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:140) ~[spring-boot-2.0.0.RELEASE.jar:2.0.0.RELEASE]

at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:752) [spring-boot-2.0.0.RELEASE.jar:2.0.0.RELEASE]

at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:388) [spring-boot-2.0.0.RELEASE.jar:2.0.0.RELEASE]

at org.springframework.boot.SpringApplication.run(SpringApplication.java:327) [spring-boot-2.0.0.RELEASE.jar:2.0.0.RELEASE]

at org.springframework.boot.SpringApplication.run(SpringApplication.java:1246) [spring-boot-2.0.0.RELEASE.jar:2.0.0.RELEASE]

at org.springframework.boot.SpringApplication.run(SpringApplication.java:1234) [spring-boot-2.0.0.RELEASE.jar:2.0.0.RELEASE]

at com.intermind.kr.Main.main(Main.java:14) [classes/:na]

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'qnaRepository': Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Not a managed type: class com.intermind.kr.model.Qna

at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1710) ~[spring-beans-5.0.4.RELEASE.jar:5.0.4.RELEASE]

at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:583) ~[spring-beans-5.0.4.RELEASE.jar:5.0.4.RELEASE]

at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:502) ~[spring-beans-5.0.4.RELEASE.jar:5.0.4.RELEASE]

at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:312) ~[spring-beans-5.0.4.RELEASE.jar:5.0.4.RELEASE]

at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228) ~[spring-beans-5.0.4.RELEASE.jar:5.0.4.RELEASE]

at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:310) ~[spring-beans-5.0.4.RELEASE.jar:5.0.4.RELEASE]

at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:200) ~[spring-beans-5.0.4.RELEASE.jar:5.0.4.RELEASE]

at org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:251) ~[spring-beans-5.0.4.RELEASE.jar:5.0.4.RELEASE]

at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1138) ~[spring-beans-5.0.4.RELEASE.jar:5.0.4.RELEASE]

at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1065) ~[spring-beans-5.0.4.RELEASE.jar:5.0.4.RELEASE]

at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:584) ~[spring-beans-5.0.4.RELEASE.jar:5.0.4.RELEASE]

... 19 common frames omitted

Caused by: java.lang.IllegalArgumentException: Not a managed type: class com.intermind.kr.model.Qna

at org.hibernate.metamodel.internal.MetamodelImpl.managedType(MetamodelImpl.java:472) ~[hibernate-core-5.2.10.Final.jar:5.2.10.Final]

at org.springframework.data.jpa.repository.support.JpaMetamodelEntityInformation.<init>(JpaMetamodelEntityInformation.java:73) ~[spring-data-jpa-2.0.5.RELEASE.jar:2.0.5.RELEASE]

at org.springframework.data.jpa.repository.support.JpaEntityInformationSupport.getEntityInformation(JpaEntityInformationSupport.java:66) ~[spring-data-jpa-2.0.5.RELEASE.jar:2.0.5.RELEASE]

at org.springframework.data.jpa.repository.support.JpaRepositoryFactory.getEntityInformation(JpaRepositoryFactory.java:181) ~[spring-data-jpa-2.0.5.RELEASE.jar:2.0.5.RELEASE]

at org.springframework.data.jpa.repository.support.JpaRepositoryFactory.getTargetRepository(JpaRepositoryFactory.java:119) ~[spring-data-jpa-2.0.5.RELEASE.jar:2.0.5.RELEASE]

at org.springframework.data.jpa.repository.support.JpaRepositoryFactory.getTargetRepository(JpaRepositoryFactory.java:102) ~[spring-data-jpa-2.0.5.RELEASE.jar:2.0.5.RELEASE]

at org.springframework.data.repository.core.support.RepositoryFactorySupport.getRepository(RepositoryFactorySupport.java:298) ~[spring-data-commons-2.0.5.RELEASE.jar:2.0.5.RELEASE]

at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.lambda$afterPropertiesSet$3(RepositoryFactoryBeanSupport.java:287) ~[spring-data-commons-2.0.5.RELEASE.jar:2.0.5.RELEASE]

at org.springframework.data.util.Lazy.getNullable(Lazy.java:141) ~[spring-data-commons-2.0.5.RELEASE.jar:2.0.5.RELEASE]

at org.springframework.data.util.Lazy.get(Lazy.java:63) ~[spring-data-commons-2.0.5.RELEASE.jar:2.0.5.RELEASE]

at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.afterPropertiesSet(RepositoryFactoryBeanSupport.java:290) ~[spring-data-commons-2.0.5.RELEASE.jar:2.0.5.RELEASE]

at org.springframework.data.jpa.repository.support.JpaRepositoryFactoryBean.afterPropertiesSet(JpaRepositoryFactoryBean.java:102) ~[spring-data-jpa-2.0.5.RELEASE.jar:2.0.5.RELEASE]

at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1769) ~[spring-beans-5.0.4.RELEASE.jar:5.0.4.RELEASE]

at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1706) ~[spring-beans-5.0.4.RELEASE.jar:5.0.4.RELEASE]

... 29 common frames omitted


jpa 엔티티 클래스를 빈으로 만들수가 없다는 내용인것 같은대 검색이 잘 안되내요..

Hibernate 설정 파일입니다.

@Configuration
@EnableTransactionManagement
@EntityScan(basePackages = "com.intermind.kr.model")
public class JpaConfig {

@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
em.setDataSource(dataSource());
em.setPackagesToScan(new String[] {"com.intermind.kr.repository"});
JpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
em.setJpaVendorAdapter(vendorAdapter);
em.setJpaProperties(additionalProperties());
return em;
}

@Bean
public DriverManagerDataSource dataSource(){
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName("org.mariadb.jdbc.Driver");
dataSource.setUrl("jdbc:mariadb://127.0.0.1:3306/db");
dataSource.setUsername("root");
dataSource.setPassword("pwd");
return dataSource;
}

@Bean
public PlatformTransactionManager transactionManager(
EntityManagerFactory emf){
JpaTransactionManager transactionManager = new JpaTransactionManager();
transactionManager.setEntityManagerFactory(emf);

return transactionManager;
}

@Bean
public PersistenceExceptionTranslationPostProcessor exceptionTranslation(){
return new PersistenceExceptionTranslationPostProcessor();
}

Properties additionalProperties() {
Properties properties = new Properties();
properties.setProperty("hibernate.hbm2ddl.auto", "none");
properties.setProperty("hibernate.dialect", "org.hibernate.dialect.MariaDBDialect");

return properties;
}
}

Entity 클래스입니다

@Data
@Entity
@Getter
@Setter
@ToString
@NoArgsConstructor
@Table(name = "qnas")
public class Qna {

@Id
@GeneratedValue
int id;

@Column(nullable = false)
int user_id;

@Column(nullable = false)
String email;

@Column(nullable = false)
String content;

@Column(nullable = false)
String type;

@Column(nullable = true)
String reply;

private java.sql.Timestamp created_at;

private java.sql.Timestamp updated_at;

int isadmin;
}

엔티티 클래스를 읽지 못하는 이유를 잘 모르겠내요...

부탁드립니다 선배님들!

0
  • 답변 8

  • bluerain
    2k
    2018-08-09 16:21:16

    2018-08-09 15:21:44.185  INFO 38558 --- [           main] org.hibernate.cfg.Environment            : HHH000206: hibernate.properties not found


    라고, 올리신 에러 내용에 있네요.

    hibernate.properties 설정 파일 위치 확인해보세요.


  • 빵상
    2018-08-09 16:30:03

    답변 감사합니다.

    우선 LocalContainerEntityManagerFactoryBean 을 생성할때

     setJpaProperties에서 프로퍼티를 만들어서 리턴해주고 있습니다.

    INFO레벨이고 그 아래 보시면 org.hibernate.dialect.MariaDBDialect

    를 additionalProperties 함수에서 셋팅해주고 있습니다.


    hibernate 프로퍼티에서 entity 위치를 읽어들이는 설정이 있나요 혹시??

  • bluerain
    2k
    2018-08-09 16:36:05

    두개의 DB 관련 Bean을 사용하시는 경우, 한쪽은 secondery 설정을 해주어야 합니다.

    별도의 설정없이 @Bean으로 잡는 경우, default로 primary로 잡히기 때문에

    어느쪽을 잡아야 하는지, 어느 것을 참조해야 하는지 찾지 못해 발생한 에러로 보입니다.

    datasource, transactionManager 등의 method 명은 같지 않게 설정하시고,

    annotation의 @bean 에 자신의 alias를 지정해 주셔야 합니다.


    아래는 hibernate 관련 설정에 참고하세요.


    https://stackoverflow.com/questions/44127381/how-to-set-jpa-entitymanagerfactory-for-persistence-unit-not-to-default

  • 빵상
    2018-08-09 16:55:20

    답변 감사합니다. 한번 해보겠습니다.

  • 빵상
    2018-08-10 09:38:32

    확인결과 같은 DB에 2개의 데이타 소스를 생성해서 문제가 되고있었습니다.

    아마 좀더 셋팅을 달리하면 될꺼 같습니다만 일단 데이터 소스 한개로 하고 처리 완료 하였습니다.

  • bluerain
    2k
    2018-08-10 09:45:16

    그 datasource의 @bean 설정에 primary 와 관련된 설정을 하고

    그 datasource를 끌어가는 manager에서 해당 datasource 의 alias를 설정해야 한다는 겁니다.

    또한, mapperscan 부분도 분리를해서 mybatis를 쓰는 경우, xml 폴더를 분리해야 하구요.

    관련 설정에 참고하실만한 url 입니다.

    (hibernate가 아닌 multi datasource 설정 관련 부분입니다.)

    http://cpdev.tistory.com/76

  • bluerain
    2k
    2018-08-10 09:47:36

    참고로 제가 진행했던 프로젝트에서는 

    oracle + mysql + mongodb 세가지를 모두 사용했습니다.

    모두 datasource를 분리했고, mybatis가 바라볼 mapper의 위치도 분리했습니다.

  • 빵상
    2018-08-10 10:11:58

    와 

    링크따라 @Primary 한쪽에만 달아주니

    같은디비도 2개 데이터소스 유지가 가능했었내요..

    답변 정말 감사합니다. 이제 2,3,4,5개 붙어도 셋팅할수 있게되었내요!


  • 로그인을 하시면 답변 을 등록할 수 있습니다.