[Spring JPA] JPA๋?
์ด๋ฒ ๊ธ์์๋ JPA(Java Persistence API)์ ๋ํด ์์๋ณด๋ ค๊ณ ํ๋ค.
JPA๋ ์๋ฐ ์ง์์์ ORM(Object-Relational Mapping) ๊ธฐ์ ํ์ค์ผ๋ก ์ฌ์ฉ๋๋ ์ธํฐํ์ด์ค ๋ชจ์์ด๋ค.
๊ทธ ๋ง์ ์ฆ, ์ค์ ์ ์ผ๋ก ๊ตฌํ ๋ ๊ฒ์ด ์๋๋ผ ๊ตฌํ๋ ํด๋์ค์ ๋งคํ์ ํด์ฃผ๊ธฐ ์ํด ์ฌ์ฉ๋๋ ํ๋ ์์ํฌ
JPA๋ฅผ ๊ตฌํํ ๋ํ์ ์ธ ์คํ์์ค๋ก๋ Hibernate๊ฐ ์๋ค.
JPA(Java Persistence API) ๋?
- Java ์ง์์์ ORM(Object-Relational Mapping) ๊ธฐ์ ํ์ค์ผ๋ก ์ฌ์ฉํ๋ ์ธํฐํ์ด์ค ๋ชจ์
- ์๋ฐ ์ดํ๋ฆฌ์ผ์ด์ ์์ ๊ด๊ณํ ๋ฐ์ดํฐ๋ฒ ์ด์ค๋ฅผ ์ฌ์ฉํ๋ ๋ฐฉ์์ ์ ์ํ ์ธํฐํ์ด์ค
- ์ธํฐํ์ด์ค ์ด๊ธฐ ๋๋ฌธ์ Hibernate, OpenJPA ๋ฑ์ด JPA๋ฅผ ๊ตฌํํจ
์ฅ์
- ๊ฐํธํ CRUD ์์
- Spring Data JPA๋ ๊ฐ๋ฐ์๊ฐ ๋ฐ์ดํฐ๋ฒ ์ด์ค์ ๋ํ CRUD ์์ ์ ์ ์ํ๋ ๋ฐ ๋์์ ์ค๋๋ค. Repository ์ธํฐํ์ด์ค๋ฅผ ํตํด ๊ฐ๋จํ ๋ฉ์๋ ์ ์ธ๋ง์ผ๋ก๋ ๊ธฐ๋ณธ์ ์ธ CRUD ์์ ์ ์ํํ ์ ์๋ค.
- ์ฟผ๋ฆฌ ๋ฉ์๋
- Spring Data JPA๋ ๋ฉ์๋ ์ด๋ฆ์ ํตํ ์ฟผ๋ฆฌ ์์ฑ์ ์ง์ํฉ๋๋ค. ๋ฉ์๋ ์ด๋ฆ๋ง์ผ๋ก๋ ์ฟผ๋ฆฌ๋ฅผ ์์ฑํ ์ ์์ด ๊ฐ๋ฐ์๊ฐ ์ง์ SQL์ ์์ฑํ์ง ์์๋ ๋๋ค.
- ์๋ํ๋ JPA ๊ตฌํ
- Spring Data JPA๋ Repository ์ธํฐํ์ด์ค์ ๋ํ ๊ตฌํ์ฒด๋ฅผ ์๋์ผ๋ก ์์ฑํฉ๋๋ค. ์ด๋ฅผ ํตํด ๊ฐ๋ฐ์๋ ๋ณ๋์ ๊ตฌํ ํด๋์ค๋ฅผ ์์ฑํ์ง ์์๋ ๋๋ค.
- ํ์ด์ง๋ค์ด์
๋ฐ ์ ๋ ฌ
- Spring Data JPA๋ ํ์ด์ง๊ณผ ์ ๋ ฌ ๊ธฐ๋ฅ์ ์ง์ํ์ฌ ๋๋์ ๋ฐ์ดํฐ๋ฅผ ๋ค๋ฃฐ ๋ ํจ๊ณผ์ ์ผ๋ก ์ฌ์ฉํ ์ ์๋ค.
- ์ ์ฐ์ฑ
- JPA ๊ธฐ๋ฐ์ ๋ค์ํ ๋ฐ์ดํฐ๋ฒ ์ด์ค๋ฅผ ์ง์ํ๋ฉฐ, ๊ฐ๋ฐ์๋ ๋ฐ์ดํฐ๋ฒ ์ด์ค๋ฅผ ๋ณ๊ฒฝํ์ง ์๊ณ ๋ ์ฝ๋๋ฅผ ์์ ํ์ฌ ๋ค๋ฅธ ๋ฐ์ดํฐ๋ฒ ์ด์ค๋ฅผ ์ฌ์ฉํ ์ ์๋ค.
๋จ์
- ๋ณต์กํ ์ฟผ๋ฆฌ ์ฒ๋ฆฌ
- ๊ฐ๋จํ ์ฟผ๋ฆฌ๋ ๋ฉ์๋ ์ด๋ฆ๋ง์ผ๋ก ์ฒ๋ฆฌํ ์ ์์ง๋ง, ๋ณต์กํ ์ฟผ๋ฆฌ๋ ํน์ ๋ฐ์ดํฐ๋ฒ ์ด์ค์ ์ข ์์ ์ธ ์ฟผ๋ฆฌ์ ๊ฒฝ์ฐ JPA Query Language (JPQL)์ด๋ Native Query๋ฅผ ์์ฑํด์ผ ํ๋ค.
- ์ฑ๋ฅ ์ค๋ฒํค๋
- ์ผ๋ถ ๋ณต์กํ ์ฟผ๋ฆฌ๋ ๋๋์ ๋ฐ์ดํฐ๋ฅผ ๋ค๋ฃฐ ๋๋ ์ฑ๋ฅ ์ค๋ฒํค๋๊ฐ ๋ฐ์ํ ์ ์์ต๋๋ค. ํนํ, ์ฌ๋ฐ๋ฅด๊ฒ ํ๋ํ์ง ์์ผ๋ฉด N+1 ๋ฌธ์ ๋ฑ์ด ๋ฐ์ํ ์ ์๋ค.
- ํ์ต ๊ณก์
- JPA ๋ฐ Spring Data JPA๋ ์ฒ์์๋ ํ์ต ๊ณก์ ์ด ์กด์ฌ
- ํนํ, ๋ณต์กํ ๋ฐ์ดํฐ ๋ชจ๋ธ์ด๋ ์ฑ๋ฅ ์ต์ ํ๋ฅผ ์ํด ์ถ๊ฐ์ ์ธ ํ์ต์ด ํ์ํ ์ ์๋ค.
- ๋์ ์ฟผ๋ฆฌ ์์ฑ์ ์ด๋ ค์
- ๋์ ์ผ๋ก ์ฟผ๋ฆฌ๋ฅผ ์์ฑํด์ผ ํ๋ ๊ฒฝ์ฐ์๋ JPQL์ด๋ Criteria API๋ฅผ ์ฌ์ฉํด์ผ ํ๋ฉฐ, ์ด๋ ํ์ต์ด ํ์ํ ๋ถ๋ถ
Spring Data JPA์ ํต์ฌ ๊ฐ๋
Repository
- Spring Data ์ ์ฅ์ ์ถ์ํ์ ์ค์ ์ธํฐํ์ด์ค
- ๊ด๋ฆฌํ ๋๋ฉ์ธ ํด๋์ค์ ๋๋ฉ์ธ ํด๋์ค์ ์๋ณ์ ์ ํ์ ์ ํ ์ธ์๋ก ์ฌ์ฉ
- ์ธํฐํ์ด์ค๋ ์ฃผ๋ก ์์ ํ ์ ํ์ ์บก์ฒํ๊ณ ์ด ์ธํฐํ์ด์ค๋ฅผ ํ์ฅํ๋ ์ธํฐํ์ด์ค๋ฅผ ๊ฒ์ํ๋ ๋ฐ ๋์์ด ๋๋ ๋ง์ปค ์ธํฐํ์ด์ค ์ญํ
- ์ธํฐํ์ด์ค๋ ๊ด๋ฆฌ๋๋ ์ํฐํฐ ํด๋์ค์ ๋ํ ์ ๊ตํ CRUD ๊ธฐ๋ฅ์ ์ ๊ณต
CrudRepository ์ํธ์์ฉ
@NoRepositoryBean
public interface CrudRepository<T, ID> extends Repository<T, ID> {
<S extends T> S save(S entity);
<S extends T> Iterable<S> saveAll(Iterable<S> entities);
Optional<T> findById(ID id);
boolean existsById(ID id);
Iterable<T> findAll();
Iterable<T> findAllById(Iterable<ID> ids);
long count();
void deleteById(ID id);
void delete(T entity);
void deleteAllById(Iterable<? extends ID> ids);
void deleteAll(Iterable<? extends T> entities);
void deleteAll();
}
- JpaRepository Interface๋ฅผ ์์ ๋ฐ์ผ๋ฉด ์ฌ์ฉ ํ ์ ์๋ ์ฃผ์ ๋ฉ์๋
- save() : ์ง์ ๋ ์ํฐํฐ๋ฅผ ์ ์ฅ
- findById() : ์ง์ ๋ ID๋ก ์๋ณ๋๋ ์ํฐํฐ๋ฅผ ๋ฐํ
- findAll() : ๋ชจ๋ ์ํฐํฐ๋ฅผ ๋ฐํ
- count() : ์ํฐํฐ ์๋ฅผ ๋ฐํ
- delete() : ์ง์ ๋ ์ํฐํฐ๋ฅผ ์ญ์
- existsById() : ์ง์ ๋ ID๋ฅผ ๊ฐ์ง ์ํฐํฐ๊ฐ ์กด์ฌํ๋์ง ์ฌ๋ถ๋ฅผ ๋ํ๋ธ๋ค.
PagingAndSortingRepository ์ํธ์์ฉ
@NoRepositoryBean
public interface PagingAndSortingRepository<T, ID> extends CrudRepository<T, ID> {
Iterable<T> findAll(Sort sort);
Page<T> findAll(Pageable pageable);
}
- Spring Data JPA์ ์ฅ์ ์ค ํ๋์ธ ํ์ด์ง๋ค์ด์ ์ด ๊ธฐ๋ณธ์ ์ผ๋ก ๊ฐ์ง๊ณ ์๋ค๊ณ ํ ์ ์๋ค.
PagingAndSortingRepository<User, Long> repository = // … get access to a bean
Page<User> users = repository.findAll(PageRequest.of(1, 20));
ํ์๋ ์ฟผ๋ฆฌ Repository
interface UserRepository extends CrudRepository<User, Long> {
long countByLastname(String lastname);
}
- Reference
https://docs.spring.io/spring-data/jpa/reference/index.html
Spring Data JPA :: Spring Data JPA
Oliver Gierke, Thomas Darimont, Christoph Strobl, Mark Paluch, Jay Bryant, Greg Turnquist Copies of this document may be made for your own use and for distribution to others, provided that you do not charge any fee for such copies and further provided that
docs.spring.io