

지금까지 테이블의 칼럼과 클래스의 속성을 1:1로 매핑했다.

그러나, 실제 모델에선 여러 개의 속성을 모아서 하나의 값으로 표현한다.

이처럼 여러 속성을 하나의 타입으로 지정할 때, @Embeddable 을 사용한다.
엔티티가 아닌 타입을 한 개 이상의 필드와 매핑할 때 사용.
@Embeddable
public class Address {
@Column(name = "addr1")
private String address1;
@Column(name = "addr2")
private String address2;
@Column(name = "zipcode")
private String zipcode;
protected Address() {}
... 생성자, getter 생략
@Entity
@Table(name = "hotel_info")
public class Hotel {
@Id
@Column(name = "hotel_id")
private String id;
...
@Embedded
private Address address;
@Embedded를 붙인다.tx.begin();
Address address = new Address("주소1", "주소2", "12345");
Hotel hotel = new Hotel("H00", "HN", 2022, Grade.S7, address);
em.persist(hotel);
tx.commit();
insert into hotel_info
(addr1, addr2, zipcode, created, grade, modified, nm, year, hotel_id)
values (?,?,?,?,?,?,?,?,?);

Hotel hotel = em.find(Hotel.class, "H00");
logger.info("주소: {}", hotel.getAddress());
select
h1_0.hotel_id, h1_0.addr1, h1_0.addr2, h1_0.zipcode, h1_0.created, h1_0.grade, h1_0.modified, h1_0.nm, h1_0.year
from
hotel_info h1_0
where
h1_0.hotel_id=?
tx.begin();
//Address address = new Address("주소1", "주소2", "12345");
//Address address = null;
Hotel hotel = new Hotel("H00", "HN", 2022, Grade.S7, address);
em.persist(hotel);
tx.commit();

@Entity
public class Employee {
@Id
private String id;
@Embedded private Address homeAddress;
@Embedded private Address workAddress;

이럴때 @AttributeOverride로 설정 재정의가 가능

insert into Employee (addr1, addr2, zipcode, waddr1, waddr2, wzipcode, id)
values (?, ?, ?, ?, ?, ?);
- 모델을 더 잘 표현할 수 있다.
- 개별 속성을 모아서 이해해서 타입으로 더 쉽게 이해가 가능하다.