Overview

μ΄μ „μ—λŠ” JPA/Hibernate λ₯Ό ν†΅ν•˜μ—¬ μ•„μ£Ό 기본적인 mapping ꡬ쑰만 λ‹€λ£¨μ—ˆμ—ˆλ‹€.

κ·ΈλŸ¬λ‚˜ Real-World μ—μ„œμ˜ database μ—μ„œλŠ” ν•˜λ‚˜μ˜ database, 즉 ν•˜λ‚˜μ˜ Schema 내에 μ—¬λŸ¬ 개의 table 듀이 μ‘΄μž¬ν•˜κ±°, 각 table μ‚¬μ΄μ˜ νŠΉλ³„ν•œ 관계가 μžˆμ„ 수 μžˆλ‹€.

이에 λŒ€ν•œ modeling 을 JPA κ°€ μˆ˜ν–‰ν•  수 μžˆλ‹€.

Advanced Mappings

Advanced Mappings μ—λŠ” λ‹€μŒ μ„Έ κ°€μ§€κ°€ μ‘΄μž¬ν•œλ‹€.

One-to-One One-to-Many, Many-to-One Many-to-Many

μš°μ„  κ°„λ‹¨ν•˜κ²Œ One-to-One mapping 을 μ‚΄νŽ΄λ³΄μž.

One-to-One mapping 은 κ°„λ‹¨ν•˜κ²Œ λ§ν•˜λ©΄, first name, last name, email λ“±μ˜ 기본적인 column 을 ν¬ν•¨ν•œ Instructor λΌλŠ” entity 에 λŒ€ν•˜μ—¬ Instructor Detail μ΄λΌλŠ”, 좔가적인 정보λ₯Ό μ €μž₯ν•˜λŠ” entity μ™€μ˜ 관계λ₯Ό μƒκ°ν•˜λ©΄ λœλ‹€.

One-to-Many mapping 은 μ΄λ¦„μ—μ„œλ„ μ•Œ 수 μžˆλ“―μ΄, ν•˜λ‚˜μ˜ entity κ°€ μ—¬λŸ¬ 개의 entity 에 mapping λ˜λŠ” ꡬ쑰이닀. 예λ₯Ό λ“€μ–΄, Instructor entity λŠ” μ—¬λŸ¬ 개의 Course entity λ₯Ό κ°€μ§ˆ 수 μžˆλ‹€λŠ” 것이닀.

λ§ˆμ§€λ§‰μœΌλ‘œ Many-to-Many λŠ” ν•˜λ‚˜μ˜ Course κ°€ μ—¬λŸ¬ 개의 Student λ₯Ό κ°€μ§ˆ 수 있으며, λ§ˆμ°¬κ°€μ§€λ‘œ Student μ—­μ‹œ μ—¬λŸ¬ 개의 Course λ₯Ό κ°€μ§ˆ 수 μžˆλŠ” ꡬ쑰λ₯Ό 예둜 λ“€ 수 μžˆλ‹€.

더 μžμ„Έν•œ λ‚΄μš©μ„ μ•Œμ•„λ³΄κΈ° 전에, Database 에 μžˆμ–΄μ„œ μ€‘μš”ν•œ concepts 인 Primary key / Foreign Key, Cascase 에 λŒ€ν•˜μ—¬ μ•Œμ•„λ³΄μž.

Primary Key and Foreign Key

Primary Key λŠ” 잘 μ•Œκ³  μžˆλ“―μ΄, table μ—μ„œ unique ν•œ row λ₯Ό identify ν•˜λŠ” 역할을 μˆ˜ν–‰ν•œλ‹€.

Foreign Key λŠ” λ‹€λ₯Έ table 의 primary key λ₯Ό μžμ‹ μ˜ table μ•ˆμ—μ„œ field 둜써 μ‚¬μš©ν•˜λŠ” 역할을 μˆ˜ν–‰ν•œλ‹€. μœ„μ™€ 같이, instructor_detail table 이 instructor table 에 μ’…μ†λ˜μ–΄ μžˆλŠ” κ΄€κ³„μ΄λ―€λ‘œ, instructor_detail 의 PK(Primary Key) λ₯Ό instructor 의 FK(Foreign Key) 둜 μ‚¬μš©ν•˜λŠ” 것이 일반적인 방법이닀.

Cascade

Cascade λ₯Ό λ²ˆμ—­ν•˜λ©΄ β€œν­ν¬μ²˜λŸΌ 흐λ₯΄λ‹€β€λΌλŠ” λœ»μ„ κ°–κ³  μžˆλŠ”λ°, 이런 의미처럼 related entities 듀에 λŒ€ν•˜μ—¬ 같은 operation 이 μ—°μ†μ μœΌλ‘œ μˆ˜ν–‰λœλ‹€λŠ” 것을 μ˜λ―Έν•œλ‹€.

μœ„μ˜ μ΄λ―Έμ§€μ²˜λŸΌ, instructor κ°€ μ €μž₯되면 instructor_detail 도 μ €μž₯되고, μ΄λŠ” μ‚­μ œμ˜ κ²½μš°μ—μ„œλ„ λ§ˆμ°¬κ°€μ§€μ΄λ‹€. μ‚­μ œμ˜ 경우, CASCADE DELETE 둜 μ•Œλ €μ Έ μžˆλ‹€.

κ·ΈλŸ¬λ‚˜ CASCADE DELETE 의 경우, Many-to-Many 의 μƒν™©μ—μ„œλŠ” μ μ ˆν•˜μ§€ μ•Šλ‹€. 예λ₯Ό λ“€μ–΄, ν•˜λ‚˜μ˜ student λ₯Ό μ‚­μ œν•˜λ €κ³  ν•  λ•Œ, 학생에 mapping 된 course 도 μ‚­μ œν•΄μ•Ό ν•˜λŠ” 것은 μ•„λ‹ˆλ‹€.

λ”°λΌμ„œ κ°œλ°œμžλŠ” 상황에 λ”°λΌμ„œ cascading 을 configure ν•΄μ•Ό ν•œλ‹€.

Fetch Types: Eager vs Lazy Loading

data λ₯Ό fetch / retrieve ν•˜λ €κ³  ν•  λ•Œ, λͺ¨λ“  것을 retrieve ν•΄μ•Ό ν•˜λŠ” 것에 λŒ€ν•œ 의문이 μžˆμ„ 수 μžˆλ‹€.

λ§Œμ•½ One-to-Many 의 상황, 예λ₯Ό λ“€μ–΄ ν•˜λ‚˜μ˜ instructor κ°€ μ—¬λŸ¬ 개의 course λ₯Ό κ°–κ³  μžˆμ„ λ•Œ, instructor λ₯Ό retrieve ν•˜λŠ” μƒν™©μ—μ„œ 그에 μ†ν•œ course data κΉŒμ§€ λͺ¨λ‘ μ¦‰μ‹œ 가져와야할 ν•„μš”κ°€ μžˆλƒλŠ” 것이닀.

이에 Eager loading 방식은 μ¦‰μ‹œ λͺ¨λ“  data λ₯Ό κ°€μ Έμ˜€λŠ” 것이고, Lazy loading 은 request κ°€ μžˆμ„ λ•Œ κ°€μ Έμ˜€λŠ” 방식이닀.

Uni-Directional

Uni-Directional 은 단방ν–₯으둜 entity 의 관계가 μ •ν•΄μ Έ μžˆμ–΄, μƒμœ„μ˜ entity 에 λ¨Όμ € μ ‘κ·Όν•΄μ•Όλ§Œ ν•˜μœ„μ˜ entity 에 μ ‘κ·Όν•  수 μžˆλŠ” 성격을 κ°–κ³  μžˆλ‹€.

Bi-Directional

Bi-Directional 은 μ–‘λ°©ν–₯으둜, μ–‘μͺ½μ˜ entity μ—μ„œ λ‹€λ₯Έ entity 에 μ ‘κ·Όν•  수 μžˆλŠ” reference κ°€ μžˆλŠ” ꡬ쑰λ₯Ό μ˜λ―Έν•œλ‹€.