Entity @oneToMany 필드 = new ArrayList<>();로 초기화 하고 난 뒤 builder 사용할 시 null pointer exception
Entity
@oneToMany
필드 = new ArrayList<>();
로 초기화 하고 난 뒤 builder
사용할 시 null pointer exception
원인
oneToMany
로 매핑한 필드를ArrayList
로 미리 초기화를 해줬다.- 그 이후 서비스 코드에서
Builder
를 통해 객체를 생성하고 난 뒤 그 필드에서null exception
이 발생. - 빌더 패턴을 이용할 경우, 미리 지정해준 값은 무시되고 자동 초기값으로 생성되는 것이었습니다.
해결 방법
- 필드에
@Builder.Default
사용 할 시Builder
를 통해 생성한 인스턴스의 기본 값을 세팅할 수 있다.
This post is licensed under CC BY 4.0 by the author.