-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
π[Docs]: μμκ΄κ³ 맀ν (#8)
- Loading branch information
Showing
1 changed file
with
154 additions
and
1 deletion.
There are no files selected for viewing
155 changes: 154 additions & 1 deletion
155
Session 07 - κ³ κΈ λ§€ν/Chapter 01 - μμκ΄κ³ 맀ν/Note.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,157 @@ | ||
# μμκ΄κ³ 맀ν | ||
> μμ±μ: @hw130 | ||
## λͺ©μ°¨ | ||
## λͺ©μ°¨ | ||
- [μμκ΄κ³ 맀ν](#μμκ΄κ³-맀ν) | ||
- [μ‘°μΈ μ λ΅](#μ‘°μΈ-μ λ΅) | ||
- [λ¨μΌ ν μ΄λΈ μ λ΅](#λ¨μΌ-ν μ΄λΈ-μ λ΅) | ||
- [ꡬν ν΄λμ€ μ λ΅](#ꡬν-ν΄λμ€-μ λ΅) | ||
- [κ²°λ‘ λ° μ 리](#κ²°λ‘ -λ°-μ 리) | ||
|
||
|
||
### μμκ΄κ³ 맀ν | ||
--- | ||
- κ°μ²΄λ μμκ΄κ³κ° μ‘΄μ¬νμ§λ§, κ΄κ³ν λ°μ΄ν°λ² μ΄μ€λ μμ κ΄κ³κ° μλ€.(λλΆλΆ) | ||
|
||
- κ·Έλλ§ μνΌνμ μλΈνμ κ΄κ³λΌλ λͺ¨λΈλ§ κΈ°λ²μ΄ κ°μ²΄ μμκ³Ό μ μ¬νλ€. | ||
|
||
- μμκ΄κ³ 맀νμ΄λΌλ κ²μ κ°μ²΄μ μμ ꡬ쑰μ DBμ μνΌνμ μλΈνμ κ΄κ³λ₯Ό 맀ννλ κ²μ΄λ€. μ¦, μνΌνμ μλΈνμ μ λ Όλ¦¬ λͺ¨λΈμ μ€μ 물리 λͺ¨λΈλ‘ λ³ννλ λ°©λ²μ΄λ€. | ||
<img width="780" alt="αα ³αα ³α α ΅α«αα £αΊ 2023-08-06 αα ©αα ₯α« 1 43 13" src="https://github.com/hw130/Algorithm_practice/assets/87763333/2ed855e0-eca0-4de4-9c6f-4f0da327de26"> | ||
#### μνΌνμ μλΈνμ λ Όλ¦¬ λͺ¨λΈμ μ€μ 물리 λͺ¨λΈλ‘ ꡬννλ λ°©λ² | ||
- κ°μ²΄λ μμμ μ§μνλ―λ‘ λͺ¨λΈλ§κ³Ό ꡬνμ΄ λκ°μ§λ§, DBλ μμμ μ§μνμ§ μμΌλ―λ‘ λ Όλ¦¬ λͺ¨λΈμ 물리 λͺ¨λΈλ‘ ꡬνν λ°©λ²μ΄ νμνλ€. | ||
- DBμ μνΌνμ μλΈνμ λ Όλ¦¬ λͺ¨λΈμ μ€μ 물리 λͺ¨λΈλ‘ ꡬννλ λ°©λ²μ μΈκ°μ§κ° μλ€. μ¬κΈ°μ μ€μν건, DBμ μ₯μμ μΈκ°μ§λ‘ ꡬννμ§λ§ JPAμμλ μ΄λ€ λ°©μμ μ ννλ 맀νμ΄ κ°λ₯νλ€. | ||
- κ°κ° ν μ΄λΈλ‘ λ³ννλ μ‘°μΈ μ λ΅(JOINED) | ||
- λ¨μΌ ν μ΄λΈ μ λ΅μ΄λΌλ λ Όλ¦¬ λͺ¨λΈμ ν ν μ΄λΈλ‘ ν©μΉλ μ λ΅(SINGLE_TABLE) | ||
- ꡬν ν΄λμ€λ§λ€ ν μ΄λΈ μ λ΅(TABLE_PER_CLASS) | ||
|
||
### μ‘°μΈ μ λ΅ | ||
--- | ||
- κ°μ₯ μ κ·ν λ λ°©λ²μΌλ‘ ꡬννλ λ°©μμ΄λ€. | ||
- μν°ν° κ°κ°μ ν μ΄λΈλ‘ λ§λ€κ³ μμ ν μ΄λΈμ΄ λΆλͺ¨ ν μ΄λΈμ κΈ°λ³Έ ν€λ₯Ό λ°μ κΈ°λ³Έ ν€ + μΈλ ν€λ‘ μ¬μ©νλ μ λ΅ | ||
- μμ ν μ΄λΈ μ€ μ΄λ ν μ΄λΈμ μ‘°νν΄μΌνλμ§ κ΅¬λΆνκΈ° μν΄ DTYPEμ΄λ κ΅¬λΆ μ»¬λΌμ μ¬μ©νλ€. | ||
- NAME, PRICEκ° ITEM ν μ΄λΈμλ§ μ μ₯λκ³ , ALBUM, MOVIE, BOOKμ΄ κ°μμ λ°μ΄ν°λ§ μ μ₯νλ€. | ||
<img width="939" alt="αα ³αα ³α α ΅α«αα £αΊ 2023-08-06 αα ©αα ₯α« 1 48 17" src="https://github.com/hw130/Algorithm_practice/assets/87763333/51166a9d-1238-4c95-b9ec-10a1845661c7"> | ||
|
||
#### μμ μ½λ | ||
``` | ||
@Entity | ||
@Inheritance(strategy = InheritanceType.JOINED) | ||
@DiscriminatorColumn(name = "DTYPE") | ||
public abstract class Item { | ||
|
||
@GeneratedValue @Id | ||
private Long id; | ||
... | ||
``` | ||
``` | ||
@Entity | ||
@DiscriminatorValue("TEST") | ||
@PrimaryKeyJoinColumn(name = "Album_ID") | ||
public class Album extends Item { | ||
|
||
private String artist; | ||
|
||
... | ||
``` | ||
- @Inheritance(strategy = InheritanceType.JOINED) | ||
- λΆλͺ¨ ν΄λμ€μ μ§μ . μ‘°μΈ μ λ΅μ΄λ―λ‘ InheritanceType.JOINED μ€μ | ||
- @DiscriminatorColumn(name = "DTYPE") | ||
- κ΅¬λΆ μ»¬λΌ μ§μ . λΆλͺ¨ ν΄λμ€μ μ μΈνλ€. νμ ν΄λμ€λ₯Ό ꡬλΆνλ μ©λμ 컬λΌμ΄λ€. κ΄λ‘λ default = DTYPEμ΄κ³ , Defaultκ°μ΄ DTYPEμ΄λ―λ‘ name μμ±μ μλ΅ κ°λ₯ | ||
- @DiscriminatorValue("TEST") | ||
- κ΅¬λΆ μ»¬λΌμ μ λ ₯ν κ° μ§μ . | ||
- νμ ν΄λμ€μ μ μΈνλ€. μν°ν°λ₯Ό μ μ₯ν λ μνΌνμ μ κ΅¬λΆ μ»¬λΌμ μ μ₯ν κ°μ μ§μ νλ€. | ||
- μ΄λ Έν μ΄μ μ μ μΈνμ§ μμ κ²½μ° κΈ°λ³Έκ°μΌλ‘ ν΄λμ€ μ΄λ¦μ΄ λ€μ΄κ°λ€. | ||
- @PrimaryKeyJoinColumn(name = "Album_ID") | ||
- Defaultλ‘ μμ ν μ΄λΈμ λΆλͺ¨ ν μ΄λΈ id 컬λΌλͺ μ κ·Έλλ‘ μ¬μ©νλ, λ³κ²½μ ν΄λΉ μ€μ κ° μΆκ° | ||
- μ₯μ | ||
- ν μ΄λΈμ μ κ·ν | ||
- μΈλ ν€ μ°Έμ‘° λ¬΄κ²°μ± μ μ½μ‘°κ±΄ νμ© κ°λ₯ | ||
- ITEMμ PKκ° ALBUM, MOVIE, BOOKμ PKμ΄μ FKμ΄λ€. κ·Έλμ λ€λ₯Έ ν μ΄λΈμμ μμ΄ν ν μ΄λΈλ§ λ°λΌλ³΄λλ‘ μ€κ³νλ κ²μ΄ κ°λ₯νλ€. | ||
- μ μ₯곡κ°μ ν¨μ¨μ μΌλ‘ μ¬μ© κ°λ₯ | ||
- ν μ΄λΈ μ κ·νλ‘ μ μ₯곡κ°μ΄ λ± νμν λ§νΌ μλΉλλ€. | ||
- λ¨μ | ||
- μ‘°νμ μ¦μ μ‘°μΈμΌλ‘ μΈν΄ μ±λ₯ μ ν κ°λ₯μ± | ||
- 볡μ‘ν μ‘°ν 쿼리 | ||
- λ°μ΄ν° λ±λ‘ μ, λλ² μ€νλλ INSERTλ¬Έ | ||
- μ 리 | ||
- μ±λ₯ μ νλΌκ³ λμ΄μμ§λ§, μ€μ λ‘λ μν₯μ΄ ν¬μ§ μλ€. | ||
- μ€νλ € μ μ₯곡κ°μ΄ ν¨μ¨ν λκΈ° λλ¬Έμ μ₯μ μ΄ ν¬λ€. | ||
- κΈ°λ³Έμ μΌλ‘λ μ‘°μΈ μ λ΅μ΄ μ μμ΄λΌκ³ 보면 λλ€. κ°μ²΄λλ μ λ§κ³ , μ κ·νλ λκ³ , κ·Έλμ μ€κ³κ° κΉλνκ² λμ¨λ€. | ||
|
||
### λ¨μΌ ν μ΄λΈ μ λ΅ | ||
--- | ||
- νλμ ν μ΄λΈμ μ¬μ©νλ©° κ΅¬λΆ μ»¬λΌ(DTYPE)μ νμ©ν΄ λ°μ΄ν°λ₯Ό νμ©νλ μ λ΅ | ||
- | ||
<img width="814" alt="αα ³αα ³α α ΅α«αα £αΊ 2023-08-06 αα ©αα ₯α« 1 48 48" src="https://github.com/hw130/Algorithm_practice/assets/87763333/39ba9e53-3eb7-4733-9c1e-728fabf34764"> | ||
#### μμ μ½λ | ||
``` | ||
@Entity | ||
@Inheritance(strategy = InheritanceType.SINGLE_TABLE) | ||
@DiscriminatorColumn(name = "DTYPE") | ||
public abstract class Item { | ||
@GeneratedValue @Id | ||
private Long id; | ||
... | ||
``` | ||
``` | ||
@Entity | ||
@DiscriminatorValue("TEST") | ||
public class Album extends Item{ | ||
private String artist; | ||
} | ||
``` | ||
- @Inheritance(strategy = InheritanceType.SINGLE_TABLE) | ||
- λΆλͺ¨ ν΄λμ€μ μ§μ . λ¨μΌ ν μ΄λΈ μ λ΅μ΄λ―λ‘ InheritanceType.SINGLE_TABLE μ€μ | ||
- μ₯μ | ||
- INSERT 쿼리λ ν λ², SELECT 쿼리λ ν λ²μ΄λ€. μ‘°μΈν νμκ° μκ³ , μ‘°ν μ±λ₯μ΄ μ’λ€. | ||
- λ¨μν μ‘°ν 쿼리 | ||
- λ¨μ | ||
- μμ μν°ν°κ° 맀νν 컬λΌμ λͺ¨λ NULL νμ© | ||
- λμ ν μ΄λΈμ΄ μ»€μ§ κ°λ₯μ±μΌλ‘ μΈν΄ μ€νλ € μ‘°ν μ±λ₯μ΄ μμ’μμ§ μ μμ | ||
|
||
### ꡬν ν΄λμ€ μ λ΅ | ||
--- | ||
- μμ μν°ν°λ§λ€ ν μ΄λΈ μμ±νλ μ λ΅ | ||
- λ°μ΄ν°λ² μ΄μ€ μ€κ³μμ ORM μ λ¬Έκ° λ λ€ μΆμ²νμ§ μλ μ λ΅ | ||
<img width="965" alt="αα ³αα ³α α ΅α«αα £αΊ 2023-08-06 αα ©αα ₯α« 1 49 16" src="https://github.com/hw130/Algorithm_practice/assets/87763333/f7ff3b1f-a354-439b-8615-94e9905b5298"> | ||
#### ꡬν μμ | ||
``` | ||
@Entity | ||
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS) | ||
@DiscriminatorColumn(name = "DTYPE") | ||
public abstract class Item { | ||
@GeneratedValue @Id | ||
private Long id; | ||
... | ||
``` | ||
``` | ||
@Entity | ||
@DiscriminatorValue("TEST") | ||
public class Album extends Item{ | ||
private String artist; | ||
} | ||
``` | ||
- @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS) | ||
- λΆλͺ¨ ν΄λμ€μ μ§μ . ꡬν ν΄λμ€λ§λ€ ν μ΄λΈ μ λ΅μ΄λ―λ‘ InheritanceType.TABLE_PER_CLASS μ€μ | ||
- μ₯μ | ||
- μλΈ νμ μ ꡬλΆν΄μ μ²λ¦¬ν λ ν¨κ³Όμ | ||
- not null μ μ½μ‘°κ±΄ μ¬μ© κ°λ₯ | ||
- λ¨μ | ||
- μ¬λ¬ μμ ν μ΄λΈ ν¨κ» μ‘°νμ μ±λ₯μ΄ λ리λ€. (UNION SQL) | ||
- μμ ν μ΄λΈμ ν΅ν©ν΄ μΏΌλ¦¬κ° μ΄λ €μ | ||
- λ³κ²½μ΄λΌλ κ΄μ μΌλ‘ μ κ·Όν λ κ΅μ₯ν μ’μ§ μλ€. | ||
- μλ₯Ό λ€μ΄, ITEMλ€μ λͺ¨λ μ μ°νλ μ½λκ° μλ€κ³ κ°μ ν λ, ITEM νμ ν΄λμ€κ° μΆκ°λλ©΄ μ μ° μ½λκ° λ³κ²½λλ€. μΆκ°λ νμ ν΄λμ€μ μ μ° κ²°κ³Όλ₯Ό μΆκ°νκ±°λ ν΄μΌ νλ€. | ||
|
||
### κ²°λ‘ λ° μ 리 | ||
--- | ||
- κΈ°λ³Έμ μΌλ‘λ μ‘°μΈ μ λ΅μ κ°μ Έκ°μ. | ||
|
||
- μ‘°μΈ μ λ΅κ³Ό λ¨μΌ ν μ΄λΈ μ λ΅μ trade offλ₯Ό μκ°ν΄μ μ λ΅μ μ ννμ. | ||
|
||
- κ΅μ₯ν μ¬ννκ³ νμ₯μ κ°λ₯μ±λ μ μΌλ©΄ λ¨μΌ ν μ΄λΈ μ λ΅μ κ°μ Έκ°μ. κ·Έλ¬λ λΉμ¦λμ€ μ μΌλ‘ μ€μνκ³ , 볡μ‘νκ³ , νμ₯λ νλ₯ μ΄ λμΌλ©΄ μ‘°μΈ μ λ΅μ κ°μ Έκ°μ. |