JPA๋?
๊ฐ๋
์ ๋ฆฌ

H2 Database
H2๋ ์๋ฐ ๊ธฐ๋ฐ์ RDBMS์ด๋ค. Browser console ์ง์, ์ ์ฉ๋(2MB), JDBC API ์ง์ ๋ฑ ๋ค์ํ ์ฅ์ ์ ๊ฐ์ง๊ณ ์๋ค. ์ต์ํ์ ๋ฆฌ์์ค๋ก ์คํ ๊ฐ๋ฅํ ๊ฒฝ๋ DB๋ก์ ํ ์คํธ์ฉ์ผ๋ก ์ฌ์ฉํ๊ธฐ ์๋ง์ DB์ด๋ค.
JPA
JPA(Java Persistence API)๋ ํ์ฌ ORM(Object Relational Mapping)์ ๊ธฐ์ ํ์ค์ผ๋ก, ์ธํฐํ์ด์ค ๋ชจ์์ด๋ค. ์ฆ, ORM์ ์ฌ์ฉํ๊ธฐ ์ํ ์ธํฐํ์ด์ค๋ฅผ ๋ชจ์๋ ๊ฒ์ด๋ผ ๋ณผ ์ ์๋ค. ORM์ ๋ํ ์๋ฐ API ๊ท๊ฒฉ์ด๋ฉฐ Hibernate, OpenJPA ๋ฑ์ด JPA๋ฅผ ๊ตฌํํ ๊ตฌํ์ฒด์ด๋ค.
Hibernate
JPA๋ฅผ ์ฌ์ฉํ๊ธฐ ์ํด์ JPA๋ฅผ ๊ตฌํํ ORM ํ๋ ์์ํฌ ์ค ํ๋์ด๋ค. Hibernate๋ JPA ๋ช
์ธ์ ๊ตฌํ์ฒด๋ก, javax.persistence.EntityManager
์ ๊ฐ์ JPA์ ์ธํฐํ์ด์ค๋ฅผ ์ง์ ๊ตฌํํ ๋ผ์ด๋ธ๋ฌ๋ฆฌ์ด๋ค.
Spring Data JPA
Spring Data JPA๋ JPA๋ฅผ ์ฐ๊ธฐ ํธํ๊ฒ ๋ง๋ค์ด ๋์ ๋ชจ๋๋ก ๊ฐ๋ฐ์๊ฐ JPA๋ฅผ ๋ ์ฝ๊ณ ํธํ๊ฒ ์ฌ์ฉํ ์ ์๋๋ก ๋์์ค๋ค. ์ด๋ JPA๋ฅผ ํ๋จ๊ณ ์ถ์ํ ์ํจ Repository๋ผ๋ ์ธํฐํ์ด์ค๋ฅผ ์ ๊ณตํจ์ผ๋ก์จ ์ด๋ฃจ์ด์ง๋ค. ์ฌ์ฉ์๊ฐ Repository ์ธํฐํ์ด์ค์ ์ ํด์ง ๊ท์น๋๋ก ๋ฉ์๋๋ฅผ ์ ๋ ฅํ๋ฉด, Spring์ด ์์์ ํด๋น ๋ฉ์๋ ์ด๋ฆ์ ์ ํฉํ ์ฟผ๋ฆฌ๋ฅผ ๋ ๋ฆฌ๋ ๊ตฌํ์ฒด๋ฅผ ๋ง๋ค์ด Bean์ผ๋ก ๋ฑ๋กํด์ค๋ค.
Spring Boot ์ค์
pom.xml
<!-- h2 database -->
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
<!-- jpa -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
application.yml
spring:
datasource:
driverClassName: org.h2.Driver
url: jdbc:h2:mem:testdb # testdb ์คํค๋ง์ mem์ธ ๋ฉ๋ชจ๋ฆฌ ๋ฐ์ดํฐ ๋ฒ ์ด์ค๋ก ๋์
username: sa
password:
sql-script-encoding: utf-8
h2:
console:
enabled: true # h2 ์ฝ์ ์ฌ์ฉ
path: /h2 # localhost:port/h2 ๋ก ์ ๊ทผ ๊ฐ๋ฅ
settings:
trace: false # Print additional trace information
web-allow-others: true # ๋ธ๋ผ์ฐ์ ๋ก ์ ๊ทผ๊ฐ๋ฅํ๊ฒ ํ๊ธฐ
jpa:
show-sql: true # sql ์ฟผ๋ฆฌ ์ฝ์ ์ถ๋ ฅ
properties:
hibernate:
format_sql: true # sql ๋ณด๊ธฐ์ข๊ฒ ์ถ๋ ฅ
generate-ddl: false # @Entity ์ด๋
ธํ
์ด์
๊ธฐ์ค์ผ๋ก DDL ์์
๋ฐฉ์ง
hibernate:
ddl-auto: validate # ๋ณ๊ฒฝ๋ ์คํค๋ง๊ฐ ์๋์ง ํ์ธ
๋ค์๊ณผ ๊ฐ์ด ์ค์ ํ๋ฉด ๊ธฐ๋ณธ์ ์ผ๋ก h2 database์ jpa๋ฅผ ์ฌ์ฉํ ์ค๋น๊ฐ ๋ค ๋์๋ค.
์ฐธ์กฐ
Last updated
Was this helpful?