Lombok Builder error

정명진·2023년 6월 30일
0
post-custom-banner

공통 DTO를 extends 한 클래스에서

@Builder

어노테이션을 사용하니 에러가 발생했다.

A class Builder is not compatible in class B Builder?

해당 오류가 무엇인가 찾아보니 확장하려는 클래스에 @Builder가 있는데 확장 한 클래스에 또 @Builder를 사용하면 발생하는 오류였다.

어떻게 해결?

상속하려는 클래스와 상속받는 클래스 모두 @SuperBuilder 항목을 추가해줬다.

@Data
@SuperBuilder
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class CommonReviewDtoWithOutChilds {
    private Long id;
    private Long parentId;
    private Long performanceId;
    private Long userId;
    private String profileImageUrl;
    private String nickname;
    private String title;
    private float starScore;
    private String createdDate;
    private String content;
    private List<String> images = new ArrayList<>();
    private Long hit;
    private Long reviewCount;

    public CommonReviewDtoWithOutChilds(Long id, Long parentId, Long performanceId, Long userId, String profileImageUrl, String nickname, String title, float starScore, String createdDate, String content, List<String> images, Long hit, Long reviewCount) {
        this.id = id;
        this.parentId = parentId;
        this.performanceId = performanceId;
        this.userId = userId;
        this.profileImageUrl = profileImageUrl;
        this.nickname = nickname;
        this.title = title;
        this.starScore = starScore;
        this.createdDate = createdDate;
        this.content = content;
        this.images = images;
        this.hit = hit;
        this.reviewCount = reviewCount;
    }

    public static CommonReviewDtoWithOutChilds toDtoFromEntity(ReviewEntity review) {
        // 날짜 이쁘게
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy.MM.dd HH:mm");
        return CommonReviewDtoWithOutChilds.builder()
                .id(review.getId())
                .parentId(ObjectUtils.isEmpty(review.getParent())? null : review.getParent().getId())
                .performanceId(review.getPerformance().getId())
                .userId(review.getUser().getId())
                .profileImageUrl(ObjectUtils.isEmpty(review.getUser().getUserImage()) ? "이미지 없음.." : review.getUser().getUserImage().getImageUrl())
                .nickname(review.getUser().getNickname())
                .starScore(review.getStarScore())
                .content(review.getContent())
                .createdDate(review.getCreatedDate().format(formatter))
                .images(new ArrayList<>())
                .hit(review.getHit())
                .reviewCount((long) review.getChildren().size())
                .build();
    }
}

@Data
@SuperBuilder
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class CommonReviewDto extends CommonReviewDtoWithOutChilds{
    private List<CommonReviewDto> childs = new ArrayList<>();

    public CommonReviewDto(Long id, Long parentId, Long performanceId, Long userId, String profileImageUrl, String nickname, String title, float starScore, String createdDate, String content, List<String> images, Long hit, Long reviewCount, List<CommonReviewDto> childs) {
        super(id, parentId, performanceId, userId, profileImageUrl, nickname, title, starScore, createdDate, content, images, hit, reviewCount);
        this.childs = childs;
    }

    public static CommonReviewDto toDtoFromEntity(ReviewEntity review) {
        // 날짜 이쁘게
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy.MM.dd HH:mm");
        return CommonReviewDto.builder()
                .id(review.getId())
                .parentId(ObjectUtils.isEmpty(review.getParent())? null : review.getParent().getId())
                .title(review.getPerformance().getTitle())
                .childs(new ArrayList<>())
                .performanceId(review.getPerformance().getId())
                .userId(review.getUser().getId())
                .profileImageUrl(ObjectUtils.isEmpty(review.getUser().getUserImage()) ? "이미지 없음.." : review.getUser().getUserImage().getImageUrl())
                .nickname(review.getUser().getNickname())
                .starScore(review.getStarScore())
                .content(review.getContent())
                .createdDate(review.getCreatedDate().format(formatter))
                .images(new ArrayList<>())
                .hit(review.getHit())
                .reviewCount((long) review.getChildren().size())
                .build();
    }
}

해결 완료!

profile
개발자로 입사했지만 정체성을 잃어가는중... 다시 준비 시작이다..
post-custom-banner

0개의 댓글