π» Entity
μ€μ DBμ ν
μ΄λΈκ³Ό 맀νλλ ν΄λμ€λ‘ id
κ°μΌλ‘ ꡬλΆμ΄λλ©°, λ‘μ§μ ν¬ν¨ν μ μμ΅λλ€.
μλμ μμλ JPAλ₯Ό νμ©ν Entity
μ½λ μ
λλ€.
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
@Entity
@Table(name = "users")
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(name = "name")
private String name;
@Column(name = "email")
private String email;
public User() {}
public User(String name, String email) {
this.name = name;
this.email = email;
}
public Long getId() {
return this.id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return this.email;
}
public void setEmail(String email) {
return this.email = email;
}
}
μ½λμμ λ³Ό μ μλ―μ΄ DBμ νλλ€μ΄ ν΄λμ€ λ΄μ μμ±μΌλ‘ λ€μ΄κ° μλκ²μ μ μ μμ΅λλ€.
π» DTO (Data Transfer Object)
κ³μΈ΅κ° λ°μ΄ν° κ΅νμ μν΄ μ¬μ©νλ κ°μ²΄λ‘ λ‘μ§μ κ°μ§ μμΌλ©°, getter/setter
λ©μλλ§ κ°λ κ°μ²΄μ
λλ€.
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
public class UserDTO {
private Long id;
private String name;
private String email;
public UserDTO(Long id, String name, String email) {
this.id = id;
this.name = name;
this.email = email;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
μ μμμμ λ³Ό μ μλ―μ΄ νΉλ³ν λ‘μ§ μμ΄ getter/setter
λ©μλλ§ μλκ²μ μ μ μμ΅λλ€.
π» VO (Value Object)
κ°μ νννλ κ°μ²΄λ‘ λ‘μ§μ ν¬ν¨ν μ μλ κ°μ²΄μ
λλ€.
VO
λ κ°μ²΄μ λΆλ³μ± 보μ₯νλ©°, μλ‘ λ€λ₯Έ μΈμ€ν΄μ€μ¬λ λ΄λΆμ μμ±κ°μ΄ κ°λ€λ©΄ κ°μ κ°μ²΄λ‘ κ°μ£Όν©λλ€. λ΄λΆ μμ±μ΄ κ°μλ κ°μ μΈμ€ν΄μ€λ‘ μ²λ¦¬νκΈ° μν΄μλ λ°λμ VO
λ΄λΆμμ equals()
μ hashCode()
λ₯Ό μ€λ²λΌμ΄λ© ν΄μ£Όμ΄μΌ ν©λλ€.
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
public class Email {
private String value;
public Email(String value) {
if (value == null) {
throw new IllegalArgumentException("Email value cannot be null");
}
if (!isValidEmail(value)) {
throw new IllegalArgumentException("Invalid email format: " + value);
}
this.value = value;
}
public String getValue() {
return value;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Email email = (Email) o;
return Objects.equals(value, email.value);
}
@Override
public int hashCode() {
return Objects.hash(value);
}
}
μ μ½λμμμ κ°μ΄ VO
λ λ‘μ§μ ν¬ν¨ν μ μμΌλ©°, setter
λ©μλλ₯Ό ꡬννμ§ μμ λΆλ³μ±μ μ μ§ν μ μμ΅λλ€. λν μμ±κ°μ΄ κ°μ κ²½μ° μΈμ€ν΄μ€λ€μ΄ λμΌν¨μ μλ €μ€ μ μλλ‘ equal
μ μ€λ²λΌμ΄λ©ν΄μ ꡬννμμ λ³Ό μ μμ΅λλ€.