๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๊ฐœ๋ฐœ/Spring

JPA, Hibernate

by 1mj 2022. 3. 8.

ORM (Object Relational Mapping)

๊ฐ์ฒด์™€ ๊ด€๊ณ„ํ˜• ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์˜ ํ…Œ์ด๋ธ”์„ ๋งคํ•‘ํ•œ๋‹ค๋Š” ์˜๋ฏธ์ด๋‹ค.

  • ๊ฐ์ฒด๋Š” ๊ฐ์ฒด๋Œ€๋กœ, ๊ด€๊ณ„ํ˜• DB๋Š” ๊ด€๊ณ„ํ˜• DB๋Œ€๋กœ ์„ค๊ณ„
  • ORM ํ”„๋ ˆ์ž„์›Œํฌ๊ฐ€ ์ค‘๊ฐ„์—์„œ ๋งคํ•‘(์—ฐ๊ฒฐ)
  • ๊ฐ์ฒด๋ฅผ ๊ด€๊ณ„ํ˜• DB ํ…Œ์ด๋ธ”์— ์ž๋™์œผ๋กœ ์˜์†ํ™”

 

JPA (Java Persistence API)

์ž๋ฐ” ORM์— ๋Œ€ํ•œ API ํ‘œ์ค€ ๋ช…์„ธ๋ฅผ ์˜๋ฏธํ•œ๋‹ค. JPA๋Š” ORM์„ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•œ ์ธํ„ฐํŽ˜์ด์Šค์˜ ๋ชจ์Œ์œผ๋กœ JPA๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ์ด๋ฅผ ๊ตฌํ˜„ํ•œ Hibernate, DataNucleus, EclipseLink ๋“ฑ๊ณผ ๊ฐ™์€ ORM ํ”„๋ ˆ์ž„์›Œํฌ๋ฅผ ์‚ฌ์šฉํ•ด์•ผ ํ•œ๋‹ค.

JPA๋Š” ๋‚ด๋ถ€์ ์œผ๋กœ JDBC API๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ SQL์„ ์ƒ์„ฑํ•˜๊ณ  ํ˜ธ์ถœํ•œ๋‹ค. ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜๊ณผ JDBC ์‚ฌ์ด์—์„œ ๋™์ž‘ํ•˜์—ฌ ๊ฐœ๋ฐœ์ž๊ฐ€ ์ง์ ‘ JDBC API๋ฅผ ์“ฐ์ง€ ์•Š๋Š”๋‹ค.

โœ” JPA์—์„œ ๊ฐ„๋‹จํ•œ CRUD ์‚ฌ์šฉํ•˜๊ธฐ
์ €์žฅ: jpa.persist(user)
์กฐํšŒ: User user = jpa.find(userId)
์ˆ˜์ •: user.setName("๋ณ€๊ฒฝ์ด")
์‚ญ์ œ: jpa.remove(user)

 

โ” JPA๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋ฌด์—‡์ด ์ข‹์„๊นŒ

JPA๊ฐ€ ์—”ํ‹ฐํ‹ฐ์˜ ๋งคํ•‘ ์ •๋ณด๋ฅผ ๋ฐ”ํƒ•์œผ๋กœ ์ ์ ˆํ•œ SQL์„ ์ƒ์„ฑํ•˜์—ฌ DB์— ๋ณด๋‚ด๊ณ  ๊ฒฐ๊ณผ๋ฅผ ๋ฐ›๋Š”๋‹ค.

์ฟผ๋ฆฌ๋ฅผ JPA๊ฐ€ ๋งŒ๋“ค์–ด์ฃผ์–ด Object์™€ RDB ๊ฐ„์˜ ํŒจ๋Ÿฌ๋‹ค์ž„ ๋ถˆ์ผ์น˜๋ฅผ ํ•ด๊ฒฐํ•œ๋‹ค.

SQL ์ค‘์‹ฌ ๊ฐœ๋ฐœ์ด ์•„๋‹Œ ๊ฐ์ฒด ์ค‘์‹ฌ ๊ฐœ๋ฐœ์ด ๊ฐ€๋Šฅํ•˜๋‹ค.

SQL์„ ์ง์ ‘ ๋‹ค๋ฃจ์ง€ ์•Š์•„ ๋ฐ˜๋ณต์ ์ธ ์ฝ”๋“œ์˜ ์ž‘์„ฑ์„ ์ค„์ผ ์ˆ˜ ์žˆ๋‹ค.

์„ฑ๋Šฅ์ด ์ข‹๋‹ค. (๊ฐ™์€ ํŠธ๋ Œ์žญ์…˜์—์„œ ๊ฐ™์€ ์—”ํ‹ฐํ‹ฐ๋ฅผ ๋ฐ˜ํ™˜ํ•ด ํ†ต์‹ ํšŸ์ˆ˜๋ฅผ ์ค„์ผ ์ˆ˜ ์žˆ๊ณ  ๋ฉ”๋ชจ๋ฆฌ์— ์Œ“๊ณ  ํ•œ ๋ฒˆ์— SQL์„ ์ „์†กํ•œ๋‹ค.)

 

Hibernate

JPA๋ฅผ ๊ตฌํ˜„ํ•œ ๋Œ€ํ‘œ์ ์ธ ORM ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์ด๋‹ค. ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์˜ ํ…Œ์ด๋ธ”๊ณผ ์—”ํ‹ฐํ‹ฐ๋ฅผ ๋งคํ•‘ํ•ด์ฃผ๊ณ  CRUD ๊ธฐ๋Šฅ์„ ๊ฐ์ฒด ์ค‘์‹ฌ์œผ๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋„๋ก ์ œ๊ณตํ•œ๋‹ค.

 

โœ” Maven or Gradle

<!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-core -->
<dependency>
	<groupId>org.hibernate</groupId>
	<artifactId>hibernate-core</artifactId>
	<version>5.4.14.Final</version>
</dependency>

 

compile("org.springframework.boot:spring-boot-starter-data-jpa")

 

โœ” ํ…Œ์ด๋ธ”๊ณผ ๋งคํ•‘๋  Entity ํด๋ž˜์Šค

 javax.persistence ํ•˜์œ„์˜ ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

import lombok.Data;
import lombok.NoArgsConstructor;

@Entity
@Table(name = "user")
@Data
@NoArgsConstructor
public class User {
  @Id
  @GeneratedValue(strategy = GenerationType.IDENTITY)
  private Long id;
  
  @Column(name = "name")
  private String name;
  
  @Column
  private String email;
}

 

 

์ฐธ๊ณ ์ž๋ฃŒ ๋ฐ ์ถœ์ฒ˜ ๐Ÿ™‡‍โ™‚๏ธ

https://girawhale.tistory.com/119

 

[JPA] JPA๋ž€? JPA๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ์ด์œ 

์ž๋ฐ”๋Š” JDBC API๋ฅผ ์‚ฌ์šฉํ•ด SQL์„ ๋ฐ์ดํ„ฐ ๋ฒ ์ด์Šค์— ์ „๋‹ฌํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์ค‘์š”ํ•˜์ง€๋งŒ SQL ์ค‘์‹ฌ์ ์ธ ๊ฐœ๋ฐœ์„ ํ•˜๊ฒŒ ๋˜๋ฉด ๋งŽ์€ ๋ฌธ์ œ๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค. JPA Java persistence API๋Š” ์ž๋ฐ”์˜ ORM ๊ธฐ์ˆ ์˜ ํ‘œ์ค€์ด๋‹ค ORMโ“ Object-Rela

girawhale.tistory.com

https://kogle.tistory.com/32

 

Hibernate : ์„ค์ •ํ•˜๊ธฐ

0. Hibernate๋Š” Java JPA๋ฅผ ๊ตฌํ˜„ํ•œ ORM ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์ด๋‹ค. 0-1 Object Relational Model์€ 0-1-1 ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์˜ Table๊ณผ Entity๋ฅผ ๋งคํ•‘ํ•ด ์ฃผ๊ณ  0-1-2 ์ฝ๊ธฐ, ์“ฐ๊ธฐ, ์—…๋ฐ์ดํŠธ, ์‚ญ์ œ ๋“ฑ์˜ ๊ธฐ๋Šฅ์„ ์‰ฝ๊ฒŒ ์ œ๊ณตํ•œ๋‹ค. 0-1-3..

kogle.tistory.com

 

๋Œ“๊ธ€