[Spring Security] No default constructor for entity 오류 (+ cannot be applied to given types; required: no arguments)

Donghwa Shin·2023년 1월 19일
0

Spring Boot

목록 보기
4/5

No default constructor for entity 오류

만들고 있는 스프링 부트 게시판에서 로그인 기능을 만들고
로그인을 하니 로그인이 되지 않고 해당 오류가 발생했다.(아이디 비밀번호 모두 정확하게 입력했음)

엔티티의 기본 생성자가 없어서 발생한 오류로

@NoArgsConstructor 를 엔티티에 추가하여 오류를 해결했다.

cannot be applied to given types; required: no arguments 오류

위처럼 @Builder를 작성하고 @NoArgsConstructor를 사용하면 오류가 발생한다고 한다.
원인으로는 모든 멤버변수를 받는 생성자가 없는 것이 이유인 것 같다.
일부 멤버변수만 갖는 생성자 함수만 존재할 경우에도 같은 오류가 발생한다.
그럴 때, @AllArgsConstructor를 사용하거나 직접 모든 변수를 받는 생성자를 만드는 방법이 있다.

Finally, applying @Builder to a class is as if you added @AllArgsConstructor(access = AccessLevel.PACKAGE) to the class and applied the @Builder annotation to this all-args-constructor. This only works if you haven't written any explicit constructors yourself.

위 내용은 Lombokd에서 @Builder의 설명 중 일부이다.

클래스에 @Builder를 적용하는 것은 @AllArgsConstructor를 추가하고 @Builder 를 적용한 것과 같다고 한다.

그리고 명시적 생성자를 작성하지 않은 경우에만 작동한다고 하니
생성자를 직접 작성하거나 @NoArgsConstructor같은 생성자를 만들어주는 어노테이션을 사용하면 자동으로 적용되는 @AllArgsConstructor가 작동하지 않아서 위의 오류가 발생하는 것 같다. (추측)

0개의 댓글