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()λ‘ μ‘°νκ° κ°λ₯νλ€.