JPA 프로그래밍 3. Value 타입 맵핑 - KwangtaekJung/inflearn-spring-data-jpa-keesun GitHub Wiki

JPA 프로그래밍: Value 타입 맵핑

  • 엔티티 타입과 Value 타입 구분

    • 식별자가 있어야 하는가.
    • 독립적으로 존재해야 하는가.
  • Value 타입 종류

    • 기본 타입 (String, Date, Boolean, ...)
    • Composite Value 타입
    • Collection Value 타입
    • 기본 타입의 콜렉션
    • 컴포짓 타입의 콜렉션
  • Composite Value 타입 맵핑

    • @Embeddable
    • @Embedded
    • @AttributeOverrides
    • @AttributeOverride
@Embeddable
public class Address {

    private String street;

    private String city;

    private String state;

    private String zipCode;

}
    // Account Entity
...
    @Embedded
    private Address address;

    @Embedded
    @AttributeOverrides({
            @AttributeOverride(name = "street", column = @Column(name = "office_street")),
            @AttributeOverride(name = "city", column = @Column(name = "office_city")),
            @AttributeOverride(name = "state", column = @Column(name = "office_state")),
            @AttributeOverride(name = "zipCode", column = @Column(name = "office_zipCode"))
    })
    private Address officeAddress;
  • 생성된 DB 확인
springdata=# select * from account;
 id | city | state | street | zip_code |         created         | office_city | office_state | office_street | office_zip_code | password | username | yes
----+------+-------+--------+----------+-------------------------+-------------+--------------+---------------+-----------------+----------+----------+-----
  1 |      |       |        |          | 2021-08-25 10:41:07.951 |             |              |               |                 | jpa      | keesun   |
  2 |      |       |        |          | 2021-08-25 10:41:07.981 |             |              |               |                 | spring   | jung     |
(2 rows)