[Java] dto vo entity

우노구나·2025년 7월 28일

DTO

데이터를 계층(Layer) 간 전송하기 위한 객체

  • 단순 데이터 컨테이너 역할.

  • 주로 Controller ↔ Service ↔ View(API) 구간에서 사용.

  • DB와는 직접적 연관이 없음.

  • Request DTO, Response DTO 등으로 나뉘기도 함.

  • 가변 객체(mutable)로 만드는 경우가 많음.

  • 데이터 교환만을 위해 사용하므로 로직을 갖지 않고, getter/setter 메소드만 갖는다.

class RGBColorDto {
   private int red;
   private int green;
   private int blue;
  
   public RGBColor(int red, int green, int blue) {
      this.red = red;
      this.green = green;
      this.blue = blue;
   }
  
   public int getRed() {
      return red;
   }
  
   public void setRed(int red) {
      this.red = red;
   }
   ...
}

VO

값 자체로 의미가 있는 불변(immutable) 객체.
(ID로 식별되는 것이 아니라 값이 같으면 동일한 객체로 취급)

  • 불변성(immutable): 값을 변경할 수 없고, 값이 다르면 다른 객체로 취급.

  • equals()와 hashCode()를 재정의해 값 기반 비교를 함.

  • 주로 주소(Address), 금액(Money) 등 값 자체가 의미 있는 경우 사용.

public class Address {
    private final String city;
    private final String street;

    public Address(String city, String street) {
        this.city = city;
        this.street = street;
    }

    // 값 비교를 위해 equals, hashCode 오버라이드
    @Override
    public boolean equals(Object o) { ... }
    @Override
    public int hashCode() { ... }
}

Entity

데이터베이스의 테이블과 직접 매핑되는 객체.

  • ORM(JPA 등)에서 @Entity 어노테이션 사용.

  • 고유 식별자(ID)를 통해 동일성을 비교.

  • 가변(mutable) 속성을 가짐 (데이터 변경 시 DB에 반영 가능).

  • 비즈니스 로직이 들어가기도 함.

@Entity
public class User {
    @Id @GeneratedValue
    private Long id;
    private String username;
    private String password;
    private String email;
}

요약

구분EntityVO (Value Object)DTO (Data Transfer Object)
주 목적DB 테이블과 매핑, 비즈니스 로직 포함값 표현 (불변, 값 자체로 동일성)데이터 전달(계층 간 전송)
식별 기준식별자(ID)값(Value)없음 (단순 데이터 컨테이너)
가변성가변(mutable)불변(immutable)보통 가변(mutable)
DB 연관성있음 (@Entity)없음없음
사용 위치Repository, ServiceEntity 내부나 Domain 모델Controller, API 응답/요청
profile
기술 블로그

0개의 댓글