WEB/Spring

[Spring-boot] 에러 정리

제이G 2022. 7. 7. 17:19

JPA 엔티티(테이블) 명시안한 오류

1. org.springframework.beans.factory.UnsatisfiedDependencyException

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'demo(메소드명)' defined in '파일경로.클래스명' : Unsatisfied dependency expressed through method 'demo(메소드명)' parameter 0; 
종속성 충족을 만족시키지 못한 오류: 특정 클래스에 있는 demo(메소드명)이름의 bean을 만드는데 실패.
원인: demo메소드의 0번째 파라미터로 인해 불만족스러운 종속성이 표현됨. --> CourseRepository에 문제가 있음을 파악

알게된 내용:

1. @Bean : Spring에게 bean을 만들라고 명시

2. Repository(SQL)역할을 하는 CourseRepository 클래스에 무언가 문제가 있음을 파악했으니 아래의 에러구문으로 넘어가보자.

 

 

2. org.springframework.beans.factory.BeanCreationException

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'courseRepository' defined in 파일경로.CourseRepository defined in @EnableJpaRepositories declared on JpaRepositoriesRegistrar.EnableJpaRepositoriesConfiguration: Invocation of init method failed

 

 

3. java.lang.IllegalArgumentException: Not a managed type

Caused by: java.lang.IllegalArgumentException: Not a managed type: 파일경로.Course

JPA는 Java언어로 DB와 데이터를 주고받을 수 있게하는 일종의 번역기의 개념이다. 이를 위해선 DB의 테이블과 SQL개념과 매핑되는 Java의 개념이 존재해야할 것이다.

1. Entity(Domain) 클래스:  테이블 역할 (1대1 매핑)

2. Repository 인터페이스:  특정테이블에 적용할 SQL 역할

 

위의 Course 클래스는 DB상의 Course 테이블 역할을 하는 Entity클래스이다. 하지만 이는 우리의 생각일뿐, 실제로 테이블(엔티티)역할을 한다고 Spring에게 알려줘야할 것이다. 따라서, 이를 명시해주는 @Entity 어노테이션을 추가해줘야한다.

 

에러1~3 결론:

최초 CommandLiner demo(CourseRepository courseRepository)가 실행되고 파라미터인 CourseRepository를 확인해보니

@SpringBootApplication
public class Week02pracApplication {

	public static void main(String[] args) {
		SpringApplication.run(Week02pracApplication.class, args);
	}
	
	@Bean
	public CommandLineRunner demo(CourseRepository repository) {
    	... }

SQL을 적용할 대상이 Course엔티티(테이블)이라고 했으면서

Course: SQL적용할 엔티티클래스(테이블)
Long: primary key의 타입
public interface CourseRepository extends JpaRepository<Course, Long>{

}

Course클래스가 테이블역할을 한다고 명시를 안해줬으니 발생한 에러로 판단된다.

 

 

 

 

 

4.  No default contructor for entity

Caused by: org.hibernate.InstantiationException: No default constructor for entity: : com.sparta.week02prac.domain.Course

 

5.