Caused by: org.hibernate.AnnotationException: Unable to find entity-class: com.example.firstproject.entity.Comment
Caused by: org.hibernate.boot.registry.classloading.spi.ClassLoadingException: Unable to load class [com.example.firstproject.entity.Comment]
Caused by: java.lang.ClassNotFoundException: Could not load requested class : com.example.firstproject.entity.Comment
1. 문제 발생
댓글 기능 구현을 위해서
Interface에 Class 를 import 하는 부분까지 코드입력을 완료했습니다.
...
public interface CommentRepository extends JpaRepository<Comment, Long> {
// 특정 게시글의 모든 댓글 조회
@Query(value =
"SELECT *" +
"FROM comment" +
"WHERE article_id = :articleId",
nativeQuery = true)
// List<Comment> findByArticleId(Long articleId); // articleId를 입력했을 때 Comment(List 묶음인)를 반환했으면 좋겠구만!!
List<Comment> finByArticleId(@Param("articleId") Long articleId);
// 특정 닉네임의 모든 댓글 조회
// List<Comment> findByNickname(String nickname); // nickname을 입력했을 때 Comment(List 묶음인)를 반환했으면 좋겠구만!!
List<Comment> findByNickname(@Param("nickname") String nickname);
}
...
@Entity
@Getter
@ToString
@AllArgsConstructor
@NoArgsConstructor
public class Comment {
@Id // id 삽입!
@GeneratedValue(strategy = GenerationType.IDENTITY) // 자동으로 id 넘버링 + db가 id를 자동으로 생성해줌
private Long id;
@ManyToOne // 여러 개의 코멘트가 하나의 게시글에 달린다. (댓글 엔티티 여러 개가 하나의 Article 에 연관됨)
@JoinColumn(name = "article_id") // 테이블에서 연결된 대상의 정보! 그니까 외래키의 이름
private Article article;
@Column // 이 어노테이션이 있어야 DB 에서 컬럼으로 인식 가능
private String nickname;
@Column
private String body;
}
(롬복 사용했습니당)
이러고 서버실행을 했는데 오류가 났습니다.
Caused by: org.hibernate.AnnotationException: Unable to find entity-class: com.example.firstproject.entity.Comment
Caused by: org.hibernate.boot.registry.classloading.spi.ClassLoadingException: Unable to load class [com.example.firstproject.entity.Comment]
Caused by: java.lang.ClassNotFoundException: Could not load requested class : com.example.firstproject.entity.Comment
*모든 오류메세지 보기*
Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2022-01-30 14:22:29.379 ERROR 9292 --- [ main] o.s.boot.SpringApplication : Application run failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]: Invocation of init method failed; nested exception is org.hibernate.AnnotationException: Unable to find entity-class: com.example.firstproject.entity.Comment
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1804) ~[spring-beans-5.3.15.jar:5.3.15]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:620) ~[spring-beans-5.3.15.jar:5.3.15]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:542) ~[spring-beans-5.3.15.jar:5.3.15]
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:335) ~[spring-beans-5.3.15.jar:5.3.15]
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234) ~[spring-beans-5.3.15.jar:5.3.15]
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:333) ~[spring-beans-5.3.15.jar:5.3.15]
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:208) ~[spring-beans-5.3.15.jar:5.3.15]
at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1154) ~[spring-context-5.3.15.jar:5.3.15]
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:908) ~[spring-context-5.3.15.jar:5.3.15]
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:583) ~[spring-context-5.3.15.jar:5.3.15]
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:145) ~[spring-boot-2.6.3.jar:2.6.3]
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:732) ~[spring-boot-2.6.3.jar:2.6.3]
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:414) ~[spring-boot-2.6.3.jar:2.6.3]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:302) ~[spring-boot-2.6.3.jar:2.6.3]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1303) ~[spring-boot-2.6.3.jar:2.6.3]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1292) ~[spring-boot-2.6.3.jar:2.6.3]
at com.example.firstProject.FirstProjectApplication.main(FirstProjectApplication.java:10) ~[main/:na]
Caused by: org.hibernate.AnnotationException: Unable to find entity-class: com.example.firstproject.entity.Comment
at org.hibernate.cfg.annotations.reflection.internal.JPAXMLOverriddenAnnotationReader.buildNamedNativeQueries(JPAXMLOverriddenAnnotationReader.java:2558) ~[hibernate-core-5.6.4.Final.jar:5.6.4.Final]
at org.hibernate.cfg.annotations.reflection.internal.JPAXMLOverriddenMetadataProvider.getDefaults(JPAXMLOverriddenMetadataProvider.java:155) ~[hibernate-core-5.6.4.Final.jar:5.6.4.Final]
at org.hibernate.annotations.common.reflection.java.JavaReflectionManager.getDefaults(JavaReflectionManager.java:242) ~[hibernate-commons-annotations-5.1.2.Final.jar:5.1.2.Final]
at org.hibernate.boot.model.source.internal.annotations.AnnotationMetadataSourceProcessorImpl$1.<init>(AnnotationMetadataSourceProcessorImpl.java:139) ~[hibernate-core-5.6.4.Final.jar:5.6.4.Final]
at org.hibernate.boot.model.source.internal.annotations.AnnotationMetadataSourceProcessorImpl.prepare(AnnotationMetadataSourceProcessorImpl.java:137) ~[hibernate-core-5.6.4.Final.jar:5.6.4.Final]
at org.hibernate.boot.model.process.spi.MetadataBuildingProcess$1.prepare(MetadataBuildingProcess.java:173) ~[hibernate-core-5.6.4.Final.jar:5.6.4.Final]
at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.complete(MetadataBuildingProcess.java:270) ~[hibernate-core-5.6.4.Final.jar:5.6.4.Final]
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.metadata(EntityManagerFactoryBuilderImpl.java:1460) ~[hibernate-core-5.6.4.Final.jar:5.6.4.Final]
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:1494) ~[hibernate-core-5.6.4.Final.jar:5.6.4.Final]
Caused by: org.hibernate.AnnotationException: Unable to find entity-class: com.example.firstproject.entity.Comment
at org.springframework.orm.jpa.vendor.SpringHibernateJpaPersistenceProvider.createContainerEntityManagerFactory(SpringHibernateJpaPersistenceProvider.java:58) ~[spring-orm-5.3.15.jar:5.3.15]
at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:365) ~[spring-orm-5.3.15.jar:5.3.15]
at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.buildNativeEntityManagerFactory(AbstractEntityManagerFactoryBean.java:409) ~[spring-orm-5.3.15.jar:5.3.15]
at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:396) ~[spring-orm-5.3.15.jar:5.3.15]
at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.afterPropertiesSet(LocalContainerEntityManagerFactoryBean.java:341) ~[spring-orm-5.3.15.jar:5.3.15]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1863) ~[spring-beans-5.3.15.jar:5.3.15]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1800) ~[spring-beans-5.3.15.jar:5.3.15]
... 16 common frames omitted
Caused by: org.hibernate.boot.registry.classloading.spi.ClassLoadingException: Unable to load class [com.example.firstproject.entity.Comment]
at org.hibernate.boot.registry.classloading.internal.ClassLoaderServiceImpl.classForName(ClassLoaderServiceImpl.java:133) ~[hibernate-core-5.6.4.Final.jar:5.6.4.Final]
at org.hibernate.boot.internal.ClassLoaderAccessImpl.classForName(ClassLoaderAccessImpl.java:67) ~[hibernate-core-5.6.4.Final.jar:5.6.4.Final]
at org.hibernate.cfg.annotations.reflection.internal.JPAXMLOverriddenAnnotationReader.buildNamedNativeQueries(JPAXMLOverriddenAnnotationReader.java:2553) ~[hibernate-core-5.6.4.Final.jar:5.6.4.Final]
... 31 common frames omitted
Caused by: java.lang.ClassNotFoundException: Could not load requested class : com.example.firstproject.entity.Comment
at org.hibernate.boot.registry.classloading.internal.AggregatedClassLoader.findClass(AggregatedClassLoader.java:210) ~[hibernate-core-5.6.4.Final.jar:5.6.4.Final]
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:588) ~[na:na]
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521) ~[na:na]
at java.base/java.lang.Class.forName0(Native Method) ~[na:na]
at java.base/java.lang.Class.forName(Class.java:398) ~[na:na]
at org.hibernate.boot.registry.classloading.internal.ClassLoaderServiceImpl.classForName(ClassLoaderServiceImpl.java:130) ~[hibernate-core-5.6.4.Final.jar:5.6.4.Final]
... 33 common frames omitted
Caused by: org.hibernate.boot.registry.classloading.spi.ClassLoadingException: Unable to load class [com.example.firstproject.entity.Comment]
Caused by: java.lang.ClassNotFoundException: Could not load requested class : com.example.firstproject.entity.Comment
> Task :FirstProjectApplication.main() FAILED
Execution failed for task ':FirstProjectApplication.main()'.
> Process 'command 'C:/Program Files/Java/jdk-11.0.12/bin/java.exe'' finished with non-zero exit value 1
* Try:
> Run with --stacktrace option to get the stack trace.
> Run with --info or --debug option to get more log output.
> Run with --scan to get full insights.
2. 원인 찾기
Unable to load class[], Unable to find entity-class: 등등으로 구글링을 시도했으나 제가 원하는 정답이 나오지 않았습니다.
그러다 저와 다른 db이지만, unable to load class 오류가 나신 분의 블로그를 발견했습니다.
https://blog.naver.com/sonmit002/221411140443
마리아db는 버전 마다 지원하는 java 버전이 다르다는 글입니다.
3. 문제 해결
저 블로그 분도 자바 버전때문에 오류가 나셨기 때문에
밑져야 본전이라 생각하고 자바버전을 11-> 8(1.8) 로 변경해줬습니다.
다시 8버전으로 spring initializer 해준 후, 댓글기능 구현 코드 -> 서버 실행 을 해줬습니다.
그리고 서버가 정상 작동합니다
환경 세팅하기 귀찮아서 8버전으로 안바꿨었는데.... 하루종일 고통받았습니다ㅠㅠ
* 출처 : '홍팍'님 유튜브_스프링부트 입문
https://www.youtube.com/watch?v=LNW-pFxLWq4&list=PLyebPLlVYXCiYdYaWRKgCqvnCFrLEANXt&index=22
* https://blog.naver.com/sonmit002/221411140443
댓글