Sample App Features

Application μ—μ„œλŠ” 기본적으둜 object 에 λŒ€ν•œ Create, Read, Update, Delete 의 κΈ°λŠ₯을 μ œκ³΅ν•œλ‹€. μ΄λŸ¬ν•œ κΈ°λŠ₯을 μ œκ³΅ν•˜λŠ” App 을 CRUD Application 이라고도 λΆ€λ₯Έλ‹€.

Data Access Obejct

DAO λŠ” Data Access Obejct, 즉 Database 접근을 μœ„ν•œ obejct 둜, database 와 interfacing ν•˜λŠ” κΈ°λŠ₯을 λ‹΄λ‹Ήν•˜λŠ” object λ˜λŠ” Design Pattern 을 μ˜λ―Έν•œλ‹€.

주둜 μœ„μ—μ„œ μ–ΈκΈ‰ν•œ CRUD μž‘μ—…μ„ μ²˜λ¦¬ν•˜λ©°, Business Logic κ³Ό Data Access Logic 을 λΆ„λ¦¬μ‹œν‚€κΈ° μœ„ν•˜μ—¬ μ‚¬μš©λœλ‹€.

DAO λŠ” λ‹€μŒ λͺ‡ κ°€μ§€μ˜ methods λ₯Ό κ°€μ§„λ‹€.

  • save(...)
  • findById(...)
  • findAll()
  • findByLastName(...)
  • update(...)
  • delete(...)
  • deleteAll()

λ˜ν•œ DAO λŠ” 이전에 JPA λ₯Ό ν†΅ν•˜μ—¬ Database 의 data λ₯Ό save ν•˜λŠ” λ°©λ²•μ—μ„œ μ–ΈκΈ‰ν•œ JPA Entity Manager λΌλŠ” 것을 ν•„μš”λ‘œ ν•˜κ²Œ λœλ‹€.

JPA Entity Manager

JPA Entity Manger λŠ” entity 듀을 saving ν•˜κ³  retrieving ν•˜κΈ° μœ„ν•œ 핡심 Interface 이자 component 이닀. 이λ₯Ό ν†΅ν•˜μ—¬ entity 의 lifecycle 을 κ΄€λ¦¬ν•˜κ³ , Database 에 λŒ€ν•œ CRUD μž‘μ—… 및 JPQL query λ₯Ό μ‹€ν–‰ν•  수 μžˆλ‹€.

λ˜ν•œ λ‹€μ‹œ JPA Entity Manager λŠ” Data Source λΌλŠ” 것을 ν•„μš”λ‘œ ν•˜κ²Œ λ˜λŠ”λ°, Data Source λŠ” Database μ™€μ˜ 연결을 κ΄€λ¦¬ν•˜κΈ° μœ„ν•œ Interface 둜, Java μ—μ„œ Database 연결을 μ„€μ •ν•˜κ³  μ œκ³΅ν•˜λŠ” 역할을 μˆ˜ν–‰ν•œλ‹€.

이듀 JPA Entity Manager 와 Data Source λŠ” application.properties file μ—μ„œμ˜ μ„€μ •(JDBC URL, user id, password λ“±)을 기반으둜 Spring Boot κ°€ μžλ™μœΌλ‘œ μƒμ„±ν•œλ‹€.

이후 κ°œλ°œμžλŠ” Spring Boot κ°€ μžλ™μœΌλ‘œ μƒμ„±ν•œ Entity Manager λ₯Ό, μ›ν•˜λŠ” DAO 에 autowire ν•˜κ±°λ‚˜ inject ν•˜μ—¬ μ‚¬μš©ν•  수 μžˆλ‹€.

Entity Manger vs JpaRepository

μš°μ„  JpaRepository λŠ” Spring Data JPA module μ—μ„œ μ œκ³΅ν•˜λŠ” Interface 둜, μ΅œμ†Œν•œμ˜ coding 으둜 Database 에 λŒ€ν•œ CRUD μž‘μ—…μ„ 맀우 κ°„λ‹¨ν•˜κ²Œ μ²˜λ¦¬ν•  수 μžˆλ„λ‘ 도와쀀닀.

λ‹€μŒμ€ Entity Manager 와 JpaRepository 에 λŒ€ν•œ 차이점이닀.

ν•­λͺ©EntityManagerJpaRepository
μ •μ˜JPA ν‘œμ€€ API 둜, entity 의 lifecycle 관리 및 DB 와 직접 μƒν˜Έμž‘μš©ν•˜λŠ” InterfaceSpring Data JPA μ—μ„œ μ œκ³΅ν•˜λŠ” Interface 둜, κΈ°λ³Έ CRUD μž‘μ—…κ³Ό Paging 및 Sorting κΈ°λŠ₯을 μžλ™μœΌλ‘œ κ΅¬ν˜„
κΈ°λŠ₯Entity μ €μž₯, μˆ˜μ •, μ‚­μ œ, 쑰회 및 JPQL μž‘μ„± κ°€λŠ₯κΈ°λ³Έ CRUD method(save,Β findById,Β findAllΒ λ“±) 와 Paging 및 Sorting κΈ°λŠ₯ 제곡
λ³΅μž‘ν•œ Query μ²˜λ¦¬λ³΅μž‘ν•œ 동적 query μž‘μ„±μ— μ ν•©ν•˜λ©°, native SQL 을 μ‚¬μš©ν•  수 있음method μ΄λ¦„μœΌλ‘œ κ°„λ‹¨ν•œ query μž‘μ„± κ°€λŠ₯ν•˜μ§€λ§Œ λ³΅μž‘ν•œ μΏΌλ¦¬λŠ” μ œν•œμ μ΄λ©°Β @Query둜 처리 κ°€λŠ₯.
μ ν•©ν•œ μ‚¬μš© μ‚¬λ‘€λ³΅μž‘ν•œ query λ‚˜ native SQL을 μ‚¬μš©ν•΄μ•Ό ν•˜λŠ” κ²½μš°κ°„λ‹¨ν•œ CRUD μž‘μ—… 및 paginateion, sorring μ²˜λ¦¬κ°€ ν•„μš”ν•œ 경우

Data Access Layer μ—μ„œ μ‚¬μš©ν•  수 μžˆλŠ” 기술 쀑 μ–΄λ–€ 것을 선택해야 ν• μ§€λŠ” project 의 spec 에 λ”°λΌμ„œ 선택해야 ν•œλ‹€. μ•„λ§ˆ 두 개 λͺ¨λ‘ μ‚¬μš©ν•˜λŠ” κ²½μš°λ„ μžˆμ„ 것이닀.