Home > Spring > ๐Ÿƒ[Spring] JPA์™€ Hibernate๋Š” ์–ด๋–ค ๊ด€๊ณ„์ธ๊ฐ€์š”?

๐Ÿƒ[Spring] JPA์™€ Hibernate๋Š” ์–ด๋–ค ๊ด€๊ณ„์ธ๊ฐ€์š”?
Spring Framework

๐Ÿƒ[Spring] JPA์™€ Hibernate๋Š” ์–ด๋–ค ๊ด€๊ณ„์ธ๊ฐ€์š”?

  • JPA(Java Persistence API)์™€ Hibernate๋Š” ๋ฐ€์ ‘ํ•œ ๊ด€๊ณ„๊ฐ€ ์žˆ์ง€๋งŒ ์„œ๋กœ ๋‹ค๋ฅธ ๊ฐœ๋…์ž…๋‹ˆ๋‹ค.
    • ๊ทธ๋“ค์˜ ๊ด€๊ณ„๋ฅผ ์ดํ•ดํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ๊ฐ๊ฐ์˜ ์ •์˜์™€ ์—ญํ• ์„ ์‚ดํŽด๋ณผ ํ•„์š”๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค.

1๏ธโƒฃ JPA(Java Persistence API).

  • JPA(Java Persistence API)๋Š” ์ž๋ฐ” ํ‘œ์ค€ ๋ช…์„ธ(Java Specification)๋กœ, ์ž๋ฐ” ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์—์„œ ๊ด€๊ณ„ํ˜• ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค(Relational Database, RDB)๋ฅผ ์‰ฝ๊ฒŒ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋„๋ก ORM(Obeject-Relational Mapping)์„ ์ œ๊ณตํ•˜๋Š” ์ธํ„ฐํŽ˜์ด์Šค(Interface) ๋ชจ์Œ์ž…๋‹ˆ๋‹ค.
  • JPA(Java Persistence API)๋Š” ์ž๋ฐ” ๊ฐœ๋ฐœ์ž์—๊ฒŒ ORM(Object-Relational Mapping) ๊ธฐ๋Šฅ์„ ์ œ๊ณตํ•˜๊ธฐ ์œ„ํ•ด ๋„์ž…๋œ ํ‘œ์ค€ API๋กœ, ํŠน์ • ๊ตฌํ˜„์ฒด๊ฐ€ ์•„๋‹™๋‹ˆ๋‹ค.
    • ์ฆ‰, JPA(Java Persistence API) ์ž์ฒด๋Š” ๊ธฐ๋Šฅ์„ ์ œ๊ณตํ•˜์ง€ ์•Š๊ณ , ORM(Object-Relational Mapping)์˜ ํ‘œ์ค€ ๊ทœ๊ฒฉ๋งŒ ์ •์˜ํ•ฉ๋‹ˆ๋‹ค.
  • JPA(Java Persistence API)๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ์ฝ”๋“œ๊ฐ€ ํŠน์ • ๊ตฌํ˜„์ฒด์— ์ข…์†๋˜์ง€ ์•Š๋„๋ก ์ถ”์ƒํ™”(Abstraction)๋œ ์ธํ„ฐํŽ˜์ด์Šค(Interface)๋ฅผ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค.

2๏ธโƒฃ Hibernate

  • Hibernate๋Š” JPA(Java Persistence API) ํ‘œ์ค€์„ ๊ตฌํ˜„ํ•œ ORM(Object-Relational Mapping) ํ”„๋ ˆ์ž„์›Œํฌ ์ค‘ ํ•˜๋‚˜์ž…๋‹ˆ๋‹ค.
    • ์ฆ‰, JPA(Java Persistence API)๊ฐ€ ์ •์˜ํ•œ ๊ทœ๊ฒฉ์„ ์—ฌ๋Ÿฌ ๊ตฌํ˜„์ฒด ์ค‘ ํ•˜๋‚˜๋กœ, ๊ฐ€์žฅ ๋„๋ฆฌ ์‚ฌ์šฉ๋˜๋Š” ๊ตฌํ˜„์ฒด์ž…๋‹ˆ๋‹ค.
  • Hibernate๋Š” JPA(Java Persistence API)๋ฅผ ๊ตฌํ˜„ํ•˜๋ฉด์„œ, ์ถ”๊ฐ€์ ์ธ ๊ธฐ๋Šฅ๋„ ์ œ๊ณตํ•˜๋Š” ๊ฐ•๋ ฅํ•œ ORM(Object-Relational Mapping) ํ”„๋ ˆ์ž„์›Œํฌ์ž…๋‹ˆ๋‹ค.
    • ์˜ˆ๋ฅผ ๋“ค์–ด, ์บ์‹ฑ, ์Šคํ‚ค๋งˆ ์ž๋™ ์ƒ์„ฑ, HQL(Hibernate Query Language) ๋“ฑ์˜ ๊ณ ์œ ํ•œ ๊ธฐ๋Šฅ์„ ํฌํ•จํ•ฉ๋‹ˆ๋‹ค.
  • Hibernate๋Š” JPA(Java Persistence API)์˜ ํ‘œ์ค€ ์ธํ„ฐํŽ˜์ด์Šค(Interface)๋ฅผ ์ง€์›ํ•˜๋ฉด์„œ๋„, ์ž์ฒด์ ์ธ API(Application Programming Interface)๋„ ํ•จ๊ป˜ ์ œ๊ณตํ•˜์—ฌ ๋” ๋งŽ์€ ๊ธฐ๋Šฅ์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•ฉ๋‹ˆ๋‹ค.

3๏ธโƒฃ ๊ด€๊ณ„ ์š”์•ฝ.

  • JPA(Java Persistence API)๋Š” ORM(Object-Relational Mapping)์˜ ํ‘œ์ค€ ์ธํ„ฐํŽ˜์ด์Šค(Interface)๋กœ์„œ, ๊ตฌํ˜„์ฒด๊ฐ€ ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค.
  • Hibernate๋Š” JPA(Java Persistence API) ํ‘œ์ค€์„ ๊ตฌํ˜„ํ•œ ๊ตฌํ˜„์ฒด ์ค‘ ํ•˜๋‚˜์ž…๋‹ˆ๋‹ค.
    • ์ฆ‰, Hibernate๋Š” JPA(Java Persistence API)์˜ ๊ทœ๊ฒฉ์„ ๋”ฐ๋ฅด๋ฉด์„œ๋„ ์ž์ฒด์ ์ธ ํ™•์žฅ ๊ธฐ๋Šฅ์„ ์ œ๊ณตํ•˜๋Š” ORM(Object-Relational Mapping) ํ”„๋ ˆ์ž„์›Œํฌ(Framework)์ž…๋‹ˆ๋‹ค.

4๏ธโƒฃ JPA์™€ Hibernate์˜ ์‚ฌ์šฉ ๋ฐฉ์‹.

1๏ธโƒฃ JPA(Java Persistence API) ์‚ฌ์šฉ.

  • JPA(Java Persistence API)๋Š” ์ธํ„ฐํŽ˜์ด์Šค(Interface)์ด๊ธฐ ๋•Œ๋ฌธ์— ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ์ฝ”๋“œ๊ฐ€ ํŠน์ • ORM(Object-Relational Mapping) ๊ตฌํ˜„์ฒด์— ์˜์กดํ•˜์ง€ ์•Š๋„๋ก ํ•ฉ๋‹ˆ๋‹ค.
    • ๊ฐœ๋ฐœ์ž๋Š” JPA(Java Persistence API) ํ‘œ์ค€์„ ๋”ฐ๋ฅด๋ฉด์„œ, Hibernate ๊ฐ™์€ ๊ตฌํ˜„์ฒด๋ฅผ ์„ ํƒํ•ด ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

๐Ÿ‘‰ ์˜ˆ์‹œ

@PersistenceContext
private EntityManager entityManager;

public void saveUser(User user) {
    entityManager.persist(user); // JPA ํ‘œ์ค€ API ์‚ฌ์šฉ
}

2๏ธโƒฃ Hibernate ์‚ฌ์šฉ

  • Hibernate๋Š” JPA๋ฅผ ๊ตฌํ˜„ํ•˜๋ฉด์„œ ์ž์ฒด์ ์ธ API(Application Programming Interface)๋„ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค.
    • ๊ฐœ๋ฐœ์ž๋Š” JPA(Java Persistence API) ํ‘œ์ค€ API(Application Programming Interface)๋ฅผ ์‚ฌ์šฉํ•˜๊ฑฐ๋‚˜, Hibernate์˜ ๊ณ ์œ ํ•œ ๊ธฐ๋Šฅ์„ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด Hibernate์˜ API(Application Programming Interface)๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

๐Ÿ‘‰ ์˜ˆ์‹œ

Session session = sessionFactory.openSession();
session.save(user); // Hibernate ๊ณ ์œ  API ์‚ฌ์šฉ

5๏ธโƒฃ ๊ฒฐ๋ก .

  • JPA(Java Persistence API)๋Š” ORM(Object-Relational Mapping)์„ ์œ„ํ•œ ํ‘œ์ค€ API์ด๊ณ , Hibernate๋Š” ๊ทธ ํ‘œ์ค€์„ ๊ตฌํ˜„ํ•œ ๊ตฌ์ฒด์ ์ธ ๊ตฌํ˜„์ฒด์ž…๋‹ˆ๋‹ค.
  • JPA(Java Persistence API)๋Š” ๋…๋ฆฝ์ ์ด์ง€๋งŒ, Hibernate๋Š” JPA๋ฅผ ๋”ฐ๋ฅด๋ฉฐ ์ถ”๊ฐ€์ ์ธ ๊ธฐ๋Šฅ์„ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค.
    • Hibernate๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด JPA(Java Persistence API) ํ‘œ์ค€ ์ธํ„ฐํŽ˜์ด์Šค๋กœ ๊ฐœ๋ฐœํ•˜๋ฉด์„œ๋„ ํ•„์š”ํ•œ ๊ฒฝ์šฐ Hibernate์˜ ๊ณ ์œ  ๊ธฐ๋Šฅ์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.