Skip to content

Entity와 DTO

Ryujy edited this page Feb 22, 2025 · 1 revision

Entity와 DTO

문제 발생 과정

Entity 와 DTO 간의 데이터 매핑 책임을 Entity에서 갖는 부분이 존재했습니다.

해결 방법

이를 발견한 튜터님의 피드백을 통해, 두 클래스의 차이를 확인하고 적절히 책임을 분리하도록 변경할 수 있었습니다.

Entity와 DTO의 차이점

  • Entity
    • 데이터베이스와 직접적으로 매핑되는 객체로, 일반적으로 ORM(Object-Relational Mapping)을 사용하여 데이터베이스 테이블과 연결됩니다.
  • DTO(Data Transfer Object)
    • 통신 및 서버 내에서 데이터 전달을 위해 사용되는 객체입니다.
    • DTO는 일반적으로 필요한 데이터만 포함되며, 클라이언트에 전달하기 적합한 형식으로 설계됩니다.

적용한 해결책

AS-IS TO-BE 모델

  • as-is
@Getter
@Builder
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@AllArgsConstructor(access = AccessLevel.PROTECTED)
@Table(name = "p_review")
@Entity
public class Review extends BaseEntity {

    @Id
    @GeneratedValue(strategy = GenerationType.UUID)
    private UUID reviewId;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "review_user_id")
    private User user;

    private int reviewScore;

    @Column(length = 1000)
    private String reviewContent;

    @Column(length = 100)
    private String reviewImageUrl;

    @ColumnDefault("true")
    private Boolean isPublic;

    private LocalDateTime deletedAt;

    private UUID deletedBy;

    public Review(CreateReviewRequestDto requestDto) {
        this.reviewScore = requestDto.getReviewScore();
        this.reviewContent = requestDto.getReviewContent();
        this.reviewImageUrl = requestDto.getReviewImageUrl();
        this.isPublic = requestDto.getIsPublic();
    }

    public ReviewResponseDto toResponseDto() {
        return ReviewResponseDto
                .builder()
                .reviewId(this.reviewId)
                .reviewScore(this.reviewScore)
                .reviewContent(this.reviewContent)
                .reviewImageUrl(this.reviewImageUrl)
                .isPublic(this.isPublic)
                .createdAt(this.createdAt)
                .build();
    }
    
    public void softDelete() {
        this.deletedAt = LocalDateTime.now();
        this.deletedBy = UUID.randomUUID();
    }

    public void modify(ModifyReviewRequestDto requestDto) {
        this.reviewScore = requestDto.getReviewScore();
        this.reviewContent = requestDto.getReviewContent();
        this.reviewImageUrl = requestDto.getReviewImageUrl();
        this.isPublic = requestDto.getIsPublic();
    }
}
  • to-be
@Getter
@Builder
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@AllArgsConstructor(access = AccessLevel.PROTECTED)
@Table(name = "p_review")
@Entity
public class Review extends BaseEntity {

	@Id
	@GeneratedValue(strategy = GenerationType.UUID)
	private UUID reviewId;

	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name = "review_user_id")
	private User user;

	@OneToOne(fetch = FetchType.LAZY)
	@JoinColumn(name = "review_order_id")
	private Order order;

	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name = "review_res_id")
	private Restaurant restaurant;

	private int reviewScore;

	@Column(length = 1000)
	private String reviewContent;

	@Column(length = 100)
	private String reviewImageUrl;

	@ColumnDefault("true")
	private Boolean isPublic;

	public void modify(ModifyReviewRequestDto requestDto) {
		this.reviewScore = requestDto.getReviewScore();
		this.reviewContent = requestDto.getReviewContent();
		this.reviewImageUrl = requestDto.getReviewImageUrl();
		this.isPublic = requestDto.getIsPublic();
	}
}
@Getter
public class ReviewResponseDto {

	private final UUID reviewId;
	private final ReviewUserResponseDto user;
	private final ReviewRestaurantResponseDto restaurant;
	private final int reviewScore;
	private final String reviewContent;
	private final String reviewImageUrl;
	private final UUID reviewOrderId;
	private final Boolean isPublic;
	private final LocalDateTime createdAt;

	@QueryProjection
	@Builder
	public ReviewResponseDto(UUID reviewId, ReviewUserResponseDto user, ReviewRestaurantResponseDto restaurant,
		int reviewScore, String reviewContent, String reviewImageUrl, UUID reviewOrderId, Boolean isPublic,
		LocalDateTime createdAt) {
		this.reviewId = reviewId;
		this.user = user;
		this.restaurant = restaurant;
		this.reviewScore = reviewScore;
		this.reviewContent = reviewContent;
		this.reviewImageUrl = reviewImageUrl;
		this.reviewOrderId = reviewOrderId;
		this.isPublic = isPublic;
		this.createdAt = createdAt;
	}

	public static ReviewResponseDto from(Review review) {
		return ReviewResponseDto
			.builder()
			.reviewId(review.getReviewId())
			.user(ReviewUserResponseDto.from(review.getUser()))
			.restaurant(ReviewRestaurantResponseDto.from(review.getRestaurant()))
			.reviewScore(review.getReviewScore())
			.reviewContent(review.getReviewContent())
			.reviewImageUrl(review.getReviewImageUrl())
			.isPublic(review.getIsPublic())
			.createdAt(review.getCreatedAt())
			.build();
	}
}
@Builder
@AllArgsConstructor
@Getter
public class CreateReviewRequestDto {
	@NotNull
	private UUID orderId;
	@NotNull
	private UUID restaurantId;
	@NotNull
	@Min(0)
	@Max(5)
	private Integer reviewScore;
	@NotBlank
	@Length(max = 1000)
	private String reviewContent;
	@Length(max = 100)
	private String reviewImageUrl;
	@NotNull
	private Boolean isPublic;

	public Review toEntity(User user, Order order, Restaurant restaurant) {
		return Review.builder()
			.user(user)
			.order(order)
			.restaurant(restaurant)
			.reviewScore(this.reviewScore)
			.reviewContent(this.reviewContent)
			.reviewImageUrl(this.reviewImageUrl)
			.isPublic(this.isPublic)
			.build();
	}
}

프로젝트 전체 적용

  • 모든 도메인에 대한 Entity 와 DTO 에 적용하였습니다.

Clone this wiki locally