※이 글은 제가 오류를 고친 해결방안으로, 정답이 아닐 수 있습니다.
@Builder
public class Post extends BaseTimeEntity {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
@OneToMany(mappedBy = "post", cascade = CascadeType.ALL)
private List<PostImg> images = new ArrayList<>(); // 임시 이미지 아이디, join 필요
===================================================
이곳에서 초기화 되지 않고 images == null
===================================================
@Builder
public Post(Member owner, String title, String content,
String category, String tradePlace, int price, String hashTag) {
this.owner = owner;
this.title = title;
this.content = content;
this.category = category;
this.tradePlace = tradePlace;
this.price = price;
this.hashTag = hashTag;
}
public void setPost(Post post) {
this.post = post;
post.getImages().add(this); // List 추가시 NullPointerException 발생
}
@Builder.Default
If a certain field/parameter is never set during a build session, then it always gets 0 / null / false. If you've put @Builder on a class (and not a method or constructor) you can instead specify the default directly on the field, and annotate the field with @Builder.Default:
@Builder.Default private final longcreated = System.currentTimeMillis();
위에 글을 해석 해보면, 만약 어떤필드가 Build session 하는 동안 설정되지 않으면, 그것들은
항상 0 / null/ false 를 갖게된다. 그리고 다음과 같이 @Builder 대신에 필드에 직접 기본값을 설정할 수 있습니다.
@Builder.Default.
private final long created = System.currentTimeMillis();
@Builder
public class Post extends BaseTimeEntity {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
@OneToMany(mappedBy = "post", cascade = CascadeType.ALL)
====================================================
@Builder.Default // **********해결방안\
private List<PostImg> images = new ArrayList<>(); // 임시 이미지 아이디, join 필요
====================================================
@Builder
public Post(Member owner, String title, String content, String category, String tradePlace, int price, String hashTag) {
this.owner = owner;
this.title = title;
this.content = content;
this.category = category;
this.tradePlace = tradePlace;