Embedded Type - thought-corner/Backend-PlayGround GitHub Wiki

Embedded Type

  • μƒˆλ‘œμš΄ κ°’ νƒ€μž…μ„ 직접 μ •μ˜ν•΄μ„œ μ‚¬μš©ν•  수 μžˆλŠ”λ° JPAμ—μ„œ 이것을 μž„λ² λ””λ“œ νƒ€μž…μ΄λΌκ³  ν•œλ‹€.

Requirements

  • Github Release ν…Œμ΄λΈ” 섀계

Design

create table github_release
(
    tech_stack   varchar(100)                  not null,
    tag_name     varchar(100)                  not null,
    name         varchar(500)                  null,
    body         mediumtext                    null,
    published_at datetime(6)                   not null,
    prerelease   tinyint(1)  default 0         not null,
    draft        tinyint(1)  default 0         not null,
    status       varchar(20) default 'PENDING' not null,
    primary key (tech_stack, tag_name)
);

1. λ³΅ν•©ν‚€μ˜ 객체지ν–₯적 μΊ‘μŠν™” (@EmbeddedId)

  • λ°μ΄ν„°λ² μ΄μŠ€μ˜ 볡합 PKλŠ” JPA μ—”ν‹°ν‹° λ‚΄μ—μ„œ 각각의 ν•„λ“œλ‘œ μ‘΄μž¬ν•  λ•Œλ³΄λ‹€ ν•˜λ‚˜μ˜ μ‹λ³„μž 객체둜 λ¬Άμ—ˆμ„ λ•Œ 더 κ°•λ ₯ν•œ 논리λ₯Ό κ°–κ²Œ λœλ‹€.
  • μ‹λ³„μžμ˜ 단일성 : μ—”ν‹°ν‹°λ₯Ό μ‹λ³„ν•˜κΈ° μœ„ν•΄ λ°˜λ“œμ‹œ 이 두 정보가 같이 ν•„μš”ν•˜λ‹€λŠ” λΉ„μ¦ˆλ‹ˆμŠ€ κ·œμΉ™μ„ λͺ…μ‹œμ μœΌλ‘œ ν‘œν˜„ν•  수 있게 λœλ‹€.
  • μž¬μ‚¬μš©μ„± : λ‹€λ₯Έ ν…Œμ΄λΈ”μ—μ„œ 이 볡합킀λ₯Ό FK둜 μ°Έμ‘°ν•΄μ•Ό ν•˜λŠ” κ²½μš°κ°€ λ°œμƒν•œλ‹€λ©΄ λ™μΌν•œ 볡합 PKλ₯Ό κ·ΈλŒ€λ‘œ μž¬μ‚¬μš©ν•˜μ—¬ 연관관계λ₯Ό λ§€ν•‘ν•  수 μžˆλ‹€.

2. 도메인 λͺ¨λΈμ˜ 응집도 ν–₯상

  • ν…Œμ΄λΈ” 컬럼 μƒμœΌλ‘œ ν‰λ©΄μ μœΌλ‘œ λ‚˜μ—΄λ˜μ–΄ μžˆμ§€λ§Œ λΉ„μ¦ˆλ‹ˆμŠ€ λ‘œμ§μƒμœΌλ‘œ μ˜λ―Έκ°€ λ°€μ ‘ν•œ ν•„λ“œλ“€μ„ κ·Έλ£Ήν™”ν•  수 μžˆλ‹€.
  • μƒνƒœμ •λ³΄ 뢄리
  • μ½”λ“œ 가독성 확보

3. JPA의 μ‹λ³„μž 관리 κ·œμΉ™ μ€€μˆ˜

  • Serializable μΈν„°νŽ˜μ΄μŠ€ κ΅¬ν˜„ : κ³΅μ‹λ¬Έμ„œλ₯Ό 보면 λ‹€μŒκ³Ό 같이 λͺ…μ‹œκ°€ λ˜μ–΄μžˆλ‹€.
  • The following rules apply for composite primary keys:
    • The primary key class must be public and must have a public no-arg constructor.
    • The access type (field- or property-based access) of a primary key class is determined by the access type of the entity for which it is the primary key unless the primary key is a embedded id and a different access type is specified. See Section Section 2.3.
    • If property-based access is used, the properties of the primary key class must be public or protected.
    • The primary key class must be serializable.
    • The primary key class must define equals and hashCode methods. The semantics of value equality for these methods must be consistent with the database equality for the database types to which the key is mapped.
    • A composite primary key must either be represented and mapped as an embeddable class (see Section 11.1.17) or must be represented as an id class and mapped to multiple fields or properties of the entity class (see Section 11.1.22).
    • If the composite primary key class is represented as an id class, the names of primary key fields or properties in the primary key class and those of the entity class to which the id class is mapped must correspond and their types must be the same.
    • A primary key that corresponds to a derived identity must conform to the rules of Section 2.4.1.
  • JPA μ‹λ³„μž ν΄λž˜μŠ€λŠ” λ°˜λ“œμ‹œ Serializable을 κ΅¬ν˜„ν•΄μ•Ό ν•˜λ©°, equals와 hashCodeλ₯Ό μž¬μ •μ˜ν•΄μ•Ό ν•œλ‹€.
  • μž„λ² λ””λ“œ νƒ€μž…μ„ μ‚¬μš©ν•˜λ©΄ 볡합킀 관리 λ‘œμ§μ„ μ—”ν‹°ν‹° μ™ΈλΆ€λ‘œ κ²©λ¦¬ν•˜κ²Œ λœλ‹€.
  • 뿐만 μ•„λ‹ˆλΌ μ˜μ†μ„± μ»¨ν…μŠ€νŠΈλŠ” @Idλ₯Ό 톡해 μ—”ν‹°ν‹°λ₯Ό κ΄€λ¦¬ν•œλ‹€. 볡합킀인 경우 객체 자체λ₯Ό Key둜 μ‚¬μš©ν•˜κΈ° λ•Œλ¬Έμ—, μž„λ² λ””λ“œ νƒ€μž…μ„ μ‚¬μš©ν•˜λ©΄ em.find()둜 μ‘°νšŒκ°€ κ°€λŠ₯ν•˜λ‹€.