Posts Spring JPA
Post
Cancel

Spring JPA

Spring JPA


๐Ÿ’ฟ JPA

image

์ž๋ฐ” ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜์—์„œ ๊ด€๊ณ„ํ˜• ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ์‹์„ ์ •์˜ํ•œ ์ธํ„ฐํŽ˜์ด์Šค

์—ฌ๊ธฐ์„œ ์ฃผ๋ชฉํ•  ์ ์€ ๋ฐ”๋กœ ์ธํ„ฐํŽ˜์ด์Šค๋ผ๋Š” ๊ฒƒ์ด๋‹ค. JPA๋Š” ํŠน์ • ๊ธฐ๋Šฅ์„ ํ•˜๋Š” ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๊ฐ€ ์•„๋‹ˆ๋‹ค. JPA๋Š” ์ž๋ฐ” ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜์—์„œ ๊ด€๊ณ„ํ˜• ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๋ฅผ ์–ด๋–ป๊ฒŒ ์‚ฌ์šฉํ•ด์•ผ ํ•˜๋Š”์ง€๋ฅผ ์ •์˜ํ•œ ๋ฐฉ๋ฒ•์ด๋‹ค.

JPA๋Š” ๋‹จ์ˆœํžˆ interface์ด๊ธฐ ๋•Œ๋ฌธ์— ๊ตฌํ˜„์ด ์—†๋‹ค. JPA๋ฅผ ์ •์˜ํ•œ javax.persistence ํŒจํ‚ค์ง€์˜ ๋Œ€๋ถ€๋ถ„์€ ๊ฐ์ข… Annotation ๊ณผ interface, enum, Exception์œผ๋กœ ์ด๋ฃจ์–ด์ ธ ์žˆ๋‹ค. JPA์˜ ํ•ต์‹ฌ์ด ๋˜๋Š” EntityManager๋Š” ์•„๋ž˜์™€ ๊ฐ™์ด javax.persistence.EntityManager๋ผ๋Š” ํŒŒ์ผ์— interface๋กœ ์ •์˜๋˜์–ด ์žˆ๋‹ค.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
package javax.persistence;

import ...
;

public interface EntityManager {

    public void persist(Object entity);

    public <T> T merge(T entity);

    public void remove(Object entity);

    public <T> T find(Class<T> entityClass, Object primaryKey);

    // More interface methods...
}

JPA์˜ 3์š”์†Œ

  1. javax.persistance ํŒจํ‚ค์ง€๋กœ ์ •์˜๋œ API ๊ทธ ์ž์ฒด
  2. JPQL(Java Persistence Query Language)
  3. ๊ฐ์ฒด/๊ด€๊ณ„ ๋ฉ”ํƒ€๋ฐ์ดํ„ฐ ___

๐Ÿ’ฟ Hibernate

hibernate๋Š” JPA์˜ ๊ตฌํ˜„์ฒด

image

EntityManagerFactory, EntityManager, EntityTransaction์„ Hibernate์—์„œ๋Š” ๊ฐ๊ฐ SessionFactory, Session, Transaction์œผ๋กœ ์ƒ์†๋ฐ›๊ณ  ๊ฐ๊ฐ Impl๋กœ ๊ตฌํ˜„ํ•œ ๊ฒƒ์ด๋‹ค.

์ฆ‰, JPA๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด์„œ ๋ฐ˜๋“œ์‹œ hibernate๋ฅผ ์‚ฌ์šฉํ•  ํ•„์š”๊ฐ€ ์—†๋‹ค.

Hibernate ๊ตฌ์กฐ

image

  1. Configurattion
    • Session Factory์—์„œ Java Application ๋ฐ Database์™€ ํ•จ๊ป˜ ์ž‘์—…ํ•  ๋•Œ ์‚ฌ์šฉ
    • It represents an entire set of mappings of an application Java Types to an SQL database.
  2. Session Factory
    • ์‚ฌ์šฉ์ž ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜์—์„œ Session Factory์— Session ๊ฐ์ฒด๋ฅผ ์š”์ฒญํ•˜๋ฉด Session Factory๋Š” Configuration ์ •๋ณด๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ Session Object๋ฅผ ์ธ์Šคํ„ด์Šคํ™” ํ•œ๋‹ค.
  3. Session
    • ์‘์šฉ ํ”„๋กœ๊ทธ๋žจ๊ณผ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ๊ฐ„์˜ ์ƒํ˜ธ์ž‘์šฉ์„ ๋‚˜ํƒ€๋‚ธ๋‹ค.(ํ•ญ์‹œ์ ์œผ๋กœ)์„ธ์…˜์˜ ์ธ์Šคํ„ด์Šค๋Š” SessionFactory Bean์œผ๋กœ ๋ถ€ํ„ฐ ๋ฐœ์ƒ
    • Session ๊ฐ์ฒด๋Š” application ๊ณผ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค(๋ฐ์ดํ„ฐ๊ฐ€ ์ €์žฅ ๋œ)์˜ ์ธํ„ฐํŽ˜์ด์Šค์ด๋‹ค.์งง์€ ์ƒ์กด์ฃผ๊ธฐ๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ์œผ๋ฉฐ, JDBC Connection ์„ ํฌ์žฅ ํ•˜๊ณ  ์žˆ๋‹ค. (์ฟผ๋ฆฌ๋ฅผ ์ง์ ‘์ ์œผ๋กœ ์ˆ˜ํ–‰ํ•˜๋Š” ๊ฒƒ ๊ฐ™๋‹ค. ๊ทธ๋ž˜์„œ ์ƒ์กด์ฃผ๊ธฐ(ํŠธ๋žœ์žญ์…˜??)๊ฐ€ ์งง์œผ๋ฉฐ, JDBC Connection ์„ ํฌ์žฅํ•œ๋‹ค๋Š” ํ‘œํ˜„์„ ํ•œ ๊ฒƒ ๊ฐ™๋‹ค.)
    • Session, EntityManager ๊ฐ™์€ ๊ฒƒ๋“ค์ด Persistent Context๋ผ๊ณ  ๋ถ€๋ฅธ๋‹ค. ๊ทธ๋ฆญ
  4. Query
    • ์‘์šฉํ”„๋กœ๊ทธ๋žจ์ด ํ•˜๋‚˜ ์ด์ƒ์˜ ์ €์žฅ๋œ ๊ฐ์ฒด(Persistence Object)์— ๋Œ€ํ•ด ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๋ฅผ ์ฟผ๋ฆฌํ•  ์ˆ˜ ์žˆ๊ฒŒํ•œ๋‹ค.
  5. First-level cache
    • ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์™€ ์ƒํ˜ธ ์ž‘์šฉํ•˜๋Š” ๋™์•ˆ Hibernate Session ๊ฐ์ฒด๊ฐ€ ์‚ฌ์šฉํ•˜๋Š” ๊ธฐ๋ณธ ์บ์‹œ๋ฅผ ๋‚˜ํƒ€๋‚ธ๋‹ค. Session cache๋ผ๊ณ ๋„ ํ•˜๋ฉฐ, ํ˜„์žฌ ์„ธ์…˜ ๋‚ด์—์„œ ๊ฐ์ฒด๋ฅผ ํ˜„์ œ Session์— ์ €์žฅํ•œ๋‹ค. Session์— ์ €์žฅ๋œ ๊ฐ์ฒด์—์„œ DB์— ํ•˜๋Š” ๋ชจ๋“  ์š”์ฒญ์€ Session cache๋ฅผ ๊ฑฐ์นœ๋‹ค. Session ๊ฐ์ฒด๊ฐ€ ํ™œ์„ฑ์ค‘์ผ ๋•Œ๋งŒ Session cache๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.
  6. Transaction
    • ๋ฐ์ดํ„ฐ์˜ ์˜์†์„ฑ์„ ๋‹ฌ์„ฑ์‹œํ‚ค๊ณ (commit) ์˜ˆ๊ธฐ์น˜ ๋ชปํ•œ ์ƒํ™ฉ์ด ๋ฐœ์ƒํ•  ๊ฒฝ์šฐ ๋กค๋ฐฑํ•  ์ˆ˜ ์žˆ๊ฒŒํ•œ๋‹ค.
  7. Persistent objects
    • Persistent objects๋Š” ๊ด€๊ณ„ํ˜• ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์˜ ํ•œ ํ•„๋“œ๋กœ์„œ ์œ ์ง€๋˜๋Š” ํ‰๋ฒ”ํ•œ ์˜ค๋ž˜๋œ Java ๊ฐ์ฒด๋“ค(POJOs)์ด๋‹ค.
    • ๊ตฌ์„ฑ ํŒŒ์ผ(hibernate.cfg.xml ๋˜๋Š” hybernate.properties)์—์„œ ๊ตฌ์„ฑํ•˜๊ฑฐ๋‚˜ @Entity ์–ด๋…ธํ…Œ์ด์…˜์œผ๋กœ ์„ ์–ธ๋  ์ˆ˜ ์žˆ๋‹ค.
  8. Second-level-cache :
    • ์—ฌ๋Ÿฌ ์„ธ์…˜์— ๊ฑธ์ณ ๊ฐ์ฒด๋ฅผ ์ €์žฅํ•˜๋Š” ๋ฐ ์‚ฌ์šฉ๋œ๋‹ค(application ๋‹จ์œ„์˜ cache).
    • Second-level-cache๋Š” ๋ช…์‹œ์ ์œผ๋กœ ์‚ฌ์šฉ๋˜๋ฉฐ, ํ•ด๋‹น ์บ์‹œ์— ๋Œ€ํ•œ ์บ์‹œ ๊ณต๊ธ‰์ž๋ฅผ ์ œ๊ณตํ•ด์•ผ ํ•œ๋‹ค.
    • ์ผ๋ฐ˜์ ์ธ Second-level-cache ๊ณต๊ธ‰์ž ์ค‘ ํ•˜๋‚˜๋กœ EhCache๊ฐ€ ์žˆ๋‹ค.

๐Ÿ’ฟ Spring-Data

image

Spring์—์„œ ์ œ๊ณตํ•˜๋Š” ๋ชจ๋“ˆ ์ค‘ ํ•˜๋‚˜๋กœ ๊ฐœ๋ฐœ์ž๊ฐ€ ๋” ์‰ฝ๊ฒŒ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋„๋ก JPA๋ฅผ ํ•œ๋‹จ๊ณ„ ๋” ์ถ”์ƒํ™” ์‹œํ‚จ Repository๋ผ๋Š” interface๋ฅผ ์ œ๊ณต

์ฆ‰, Spring-Data-JPA์˜ Repository์˜ ๋‚ด๋ถ€์ ์ธ ๊ตฌํ˜„์—์„œ JPA๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค๋Š” ๋œป


๐Ÿ’ฟ JDBC

Java Database Connectivity์˜ ์ค„์ž„๋ง. java์—์„œ DB์— ์ ‘์†ํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•˜๋Š” API

image

  • JDBC Driver Manager๋Š” DB์— ๋งž๋Š” Driver๋ฅผ loadํ•˜์—ฌ JDBC๋ฅผ ์ดˆ๊ธฐํ™” ํ•ฉ๋‹ˆ๋‹ค
  • JDBC Driver๋“ค์€ Java App์˜ ์š”์ฒญ๋“ค์„ DBMS๊ฐ€ ์ดํ•ด ํ•  ์ˆ˜ ์žˆ๋Š” Protocal๋กœ ๋ณ€ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

image

This post is licensed under CC BY 4.0 by the author.

Thread Process

Spring JPA 2