@Entity
public class Member {
@Id @GeneratedValue
@Column(name = "MEMBER_ID")
private Long id;
@Column(name = "USERNAME")
private String username;
private LocalDateTime startDate;
private LocalDateTime endDate;
private String city;
private String street;
private String zipcode;
}
보면
startDate와 endDate
city와 street와 zipcode
이 두 그룹은 연관이 있어 묶어서 관리하면 관리도 쉽고 변경할 일이 있을 때 쉽게 바꿀 수 있을 것이다.
이 때 사용하는 것이 임베디드 타입이다.
바꾼 모습을 보면
@Entity
public class Member {
@Id @GeneratedValue
@Column(name = "MEMBER_ID")
private Long id;
@Column(name = "USERNAME")
private String username;
//기간 Period
@Embedded
private Period workPeriod;
//주소
@Embedded
private Address homeAddress;
}
@Embeddable
public class Period {
private LocalDateTime startDate;
private LocalDateTime endDate;
public Period() {
}
public Period(LocalDateTime startDate, LocalDateTime endDate) {
this.startDate = startDate;
this.endDate = endDate;
}
}
@Embeddable
public class Address {
private String city;
private String street;
private String zipcode;
public Address() {
}
public Address(String city, String street, String zipcode) {
this.city = city;
this.street = street;
this.zipcode = zipcode;
}
}
이렇게 바꾸면 된다.
여기서 @Embeddable과 @Embedded는 둘 중에서 하나만 사용하면 되지만 둘다 사용하는 것을 권장한다고 한다.
그리고 기본 생성자는 필수라고 한다.
[ JPA ] jpql 페이징 (0) | 2022.01.29 |
---|---|
[ JPA ] 값 타입 리스트 동작 방식 (0) | 2022.01.28 |
[ JPA ] CASCADE는 어디에 사용할까 (persist 한 번만 쓰고 싶을 때) (0) | 2022.01.22 |
[ JPA ] 지연로딩과 즉시로딩 (0) | 2022.01.20 |
[ JPA ] 프록시와 getReference() (0) | 2022.01.17 |