본 포스팅은 인프러의 JPA 기본편을 수강하고 정리하는 내용입니다.
실전 예제 - 6. 값 타입 매핑
- equals() 와 hashCode() 메소드를 구현할 때 getter로 멤버변수에 접근하는게 좋다.
- 프록시도 안정적으로 접근할 수 있도록
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
@Embeddable
public class Address {
@Column(length = 10)
private String city;
@Column(length = 20)
private String street;
@Column(length = 5)
private String zipcode;
private String fullAddress() {
return getCity() + " " + getStreet() + " " + getZipcode();
}
public boolean isValid() {
// 검증 로직
return true;
}
@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());
}
...
}
@Entity
public class Member extends BaseEntity {
@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<>();
...
}
@Entity
public class Delivery extends BaseEntity {
@Id
@GeneratedValue
private Long id;
@Embedded
private Address address;
private DeliveryStatus status;
@OneToOne(mappedBy = "delivery", fetch = LAZY)
private Order order;
...
}