오류 발생

스프링부트에서 Author 라는 Entity와 AuthorRepository라는 Repository를 호출하는 코드를 실행했을 때 다음 오류가 발생했다.

  • org.springframework.beans.factory.UnsatisfiedDependencyException
  • Caused by: org.springframework.beans.factory.BeanCreationException
  • Caused by: java.lang.IllegalArgumentException



오류 상세

보다 자세한 오류 내용은 다음과 같다.
3단계에 걸쳐 실제 호출해서 오류가 발생한 지점부터 근원지까지의 오류 발생 내용이 기록되어있다.
오류 내용을 제외한 나머지 내용은 길어서 생략했다.

2024-03-05T09:06:06.761+09:00 ERROR 44688 --- [main] o.s.boot.SpringApplication: Application run failed

# 1번 에러
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'bootstrapData' defined in file [/Users/iksflow/$$$$$]: Unsatisfied dependency expressed through constructor parameter 0: Error creating bean with name 'authorRepository' defined in com.example.repositories.AuthorRepository defined in @EnableJpaRepositories declared on JpaRepositoriesRegistrar.EnableJpaRepositoriesConfiguration: Not a managed type: class com.example.domain.Author
........생략........

# 2번 에러
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'authorRepository' defined in com.example.repositories.AuthorRepository defined in @EnableJpaRepositories declared on JpaRepositoriesRegistrar.EnableJpaRepositoriesConfiguration: Not a managed type: class com.example.domain.Author
........생략........

# 3번 에러
Caused by: java.lang.IllegalArgumentException: Not a managed type: class com.example.domain.Author
    at org.hibernate.metamodel.model.domain.internal.JpaMetamodelImpl.managedType(JpaMetamodelImpl.java:181) ~[hibernate-core-6.1.6.Final.jar:6.1.6.Final]
........생략........



원인 분석

1번 에러는 스프링부트 애플리케이션 실행에 실패했다는 내용이다.
오류의 결과에 해당하는 부분으로 원인을 분석하려면 2번 에러, 3번 에러를 확인해보면 찾을 수 있다.

2번 에러는 Bean 생성에 실패했다고 알려준다. 실패한 이유는 authorRespository에 정의한 Author라는 클래스가 Managed Type이 아니기 때문이다.
3번 에러는 Author 클래스가 Managed Type이 아니라고 한다.
정리하자면 이 모든 일이 Author 클래스가 Managed Type이 아니기 때문에 발생한 것이다.

문제의 원인인 Author 클래스는 아래와 같이 작성된 상태였다.


public class Author {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    private String firstName;
    private String lastName;

    @ManyToMany(mappedBy = "authors")
    private Set<Book> books = new HashSet<>();
    ... 생략 ...
}

그렇다. 3번 오류는 Author 클래스에 @Entity 애너테이션을 빠뜨려서 벌어진 일이다.



해결 방법

Author 클래스에 @Entity 애너테이션을 추가하면 Managed Type으로 인식해 정상적으로 동작한다.

@Entity
public class Author {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    private String firstName;
    private String lastName;

    @ManyToMany(mappedBy = "authors")
    private Set<Book> books = new HashSet<>();
    ... 생략 ...
}



참고 자료

-