# 실전 예제

![](/files/-MXLYZwL6-TTb6nC7hHy)

* 주소를 값 타입으로 정의한다.

{% tabs %}
{% tab title="Address.java" %}

```java
@Embeddable
@Getter
@Setter
public class Address {

    private String city;
    private String street;
    private String zipcode;

    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (o == null || getClass() != o.getClass()) {
            return false;
        }
        Address address = (Address) o;
        return Objects.equals(getCity(), address.getCity()) && Objects
                .equals(getStreet(), address.getStreet()) && Objects
                .equals(getZipcode(), address.getZipcode());
    }

    @Override
    public int hashCode() {
        return Objects.hash(getCity(), getStreet(), getZipcode());
    }
}
```

{% endtab %}
{% endtabs %}

![](/files/-MXLYZwSsxQcPgD0PBSC)

* `equals()` 재정의
  * `Use getters` 옵션을 체크해 필드에 직접 접근하지 않고 getter로 접근하게 만든다.
  * 프록시일 때는 getter로만 진짜 객체에 접근할 수 있기 때문이다.

{% tabs %}
{% tab title="Delivery.java" %}

```java
@Entity
public class Delivery {
    @Id
    @GeneratedValue
    private Long id;

    @Embedded
    private Address address;

    private DeliveryStatus status;

    @OneToOne(mappedBy = "delivery", fetch = FetchType.LAZY)
    private Order order;
}
```

{% endtab %}

{% tab title="Member.java" %}

```java
@Entity
public class Member {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "member_id")
    private Long id;
    private String name;

    @Embedded
    private Address address;

    @OneToMany(mappedBy = "member")
    private List<Order> orders = new ArrayList<>();
}
```

{% endtab %}
{% endtabs %}

![](/files/-MXLYZwZsu8WM8VgkZkz)

![](/files/-MXLYZwc9-C736DCuQ6q)

`member`와 `delivery`에 Address 값 타입이 생성되었다.

{% tabs %}
{% tab title="Address.java" %}

```java
@Embeddable
public class Address {

    // 조건을 추가할 수 있다.
    @Column(length = 10)
    private String city;

    @Column(length = 20)
    private String street;

    @Column(length = 5)
    private String zipcode;

    // 의미 있는 비즈니스 메서드를 만들 수 있다.
    public String fullAddress() {
        return getCity() + getStreet() + getZipcode();
    }
}
```

{% endtab %}
{% endtabs %}

![](/files/-MXLYZwkH41wLkk6VmV3)

* 값 타입을 만들면 공통으로 값을 관리할 수 있다.
  * 조건을 넣거나 의미있는 메서드를 만들 수 있어 편리하다.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://dodeon.gitbook.io/study/kimyounghan-orm-jpa/09-value-type/example.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
