Skip to content

Latest commit

ย 

History

History
141 lines (112 loc) ยท 7.3 KB

JPA ์„ค์ •.md

File metadata and controls

141 lines (112 loc) ยท 7.3 KB

JPA ์„ค์ •

JPA yml ์„ค์ •

  • database-plaform : ์–ด๋–ค ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์— ์ ‘์†ํ•  ๊ฒƒ์ธ์ง€์— ๋Œ€ํ•œ ๋ช…์‹œ์  ์„ค์ •์ด ๊ฐ€๋Šฅํ•˜๋‹ค.
  • open-in-view : true์ด๋ฉด ๋ทฐ ๋ Œ๋”๋ง ์‹œ์ ์— ์˜์†์„ฑ ์ปจํ…์ŠคํŠธ๊ฐ€ ์กด์žฌํ•˜์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์— ์ค€์˜์† ์ƒํƒœ์˜ ๊ฐ์ฒด์˜ ํ”„๋ก์‹œ๋ฅผ ์ดˆ๊ธฐํ™”ํ•  ์ˆ˜ ์—†๋‹ค๋ฉด ์˜์†์„ฑ ์ปจํ…์ŠคํŠธ๋ฅผ ์˜คํ”ˆ๋œ ์ฑ„๋กœ ๋ทฐ ๋ Œ๋”๋ง ์‹œ์ ๊นŒ์ง€ ์œ ์ง€ํ•˜์—ฌ ๋ทฐ ๋ Œ๋”๋ง ์‹œ์ ์—์„œ๋„ ์˜์†์„ฑ ์ปจํ…์ŠคํŠธ๋ฅผ ๋‹ค๋ฃฐ ์ˆ˜ ์žˆ๊ฒŒ ๋‹ค. ์ฆ‰, ์ž‘์—… ๋‹จ์œ„๋ฅผ ์š”์ฒญ ์‹œ์ž‘ ์‹œ์ ๋ถ€ํ„ฐ ๋ทฐ ๋ Œ๋”๋ง ์™„๋ฃŒ ์‹œ์ ๊นŒ์ง€๋กœ ํ™•์žฅํ•œ๋‹ค.
  • generate-ddl : true์ด๋ฉด ์ตœ์ดˆ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์Šคํ‚ค๋งˆ๋ฅผ ์ž๋™ ์ƒ์„ฑํ•œ๋‹ค.

ํ•˜์ด๋ฒ„๋„ค์ดํŠธ ์ „์šฉ ์†์„ฑ

  • hibernate.show_sql : ํ•˜์ด๋ฒ„๋„ค์ดํŠธ๊ฐ€ ์‹คํ–‰ํ•œ SQL์„ ์ถœ๋ ฅํ•œ๋‹ค.
  • hibernate.format_sql : ํ•˜์ด๋ฒ„๋„ค์ดํŠธ๊ฐ€ ์‹คํ–‰ํ•œ SQL์„ ์ถœ๋ ฅํ•  ๋•Œ ๋ณด๊ธฐ ์‰ฝ๊ฒŒ ์ •๋ ฌํ•ด์ค€๋‹ค.
  • hibernate.use_sql_comments : ์ฟผ๋ฆฌ๋ฅผ ์ถœ๋ ฅํ•  ๋•Œ ์ฃผ์„๋„ ํ•จ๊ป˜ ์ถœ๋ ฅํ•œ๋‹ค.
  • hibernate.id.new_generator_mappings : JPA ํ‘œ์ค€์— ๋งž์ถ˜ ์ƒˆ๋กœ์šด ํ‚ค ์ƒ์„ฑ ์ „๋žต์„ ์‚ฌ์šฉํ•œ๋‹ค.
  • hibernate.ddl-auto
    • create : ์ดˆ๊ธฐํ™” ๊ณผ์ •์—์„œ ํ…Œ์ด๋ธ”์„ ์ƒ์„ฑํ•œ๋‹ค. ๋งŒ์•ฝ ๊ธฐ์กด์— ์žˆ์—ˆ๋‹ค๋ฉด ์‚ญ์ œ ํ›„ ์ƒ์„ฑํ•œ๋‹ค.
    • create-drop : ์ดˆ๊ธฐํ™” ๊ณผ์ •์—์„œ ํ…Œ์ด๋ธ”์„ ์ƒ์„ฑํ•˜๊ณ , ์ข…๋ฃŒ ์‹œ์ ์— ๋งŒ๋“ค์–ด์ง„ ํ…Œ์ด๋ธ”์„ ์‚ญ์ œํ•œ๋‹ค. Unit Test ์šฉ์œผ๋กœ ๋งŽ์ด ์‚ฌ์šฉ๋œ๋‹ค.
    • update : ์ปฌ๋Ÿผ ์ถ”๊ฐ€/์‚ญ์ œ์™€ ๊ฐ™์ด ํ…Œ์ด๋ธ” ๋ณ€๊ฒฝ ์‚ฌํ•ญ์— ๋Œ€ํ•ด์„œ ์กฐ์น˜ํ•˜๊ฒŒ ํ•œ๋‹ค. ํ…Œ์ด๋ธ” ์ƒ์„ฑ/์‚ญ์ œ๋Š” ๊ด€์—ฌํ•˜์ง€ ์•Š๋Š”๋‹ค.
    • validate : Entity์™€ Table Mapping ์˜ค๋ฅ˜๋งŒ ์ ๊ฒ€ํ•˜๊ณ , ์˜ค๋ฅ˜ ๋ฐœ๊ฒฌ ์‹œ์— Exception์„ ๋ฐœ์ƒ์‹œํ‚จ๋‹ค.
    • none : none
  • hibernate.physical_naming_strategy : Java Code๋‚ด์— Entity์™€ Attribute๋ฅผ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์˜ ํ…Œ์ด๋ธ”๊ณผ ์ปฌ๋Ÿผ์œผ๋กœ Mapping ํ•  ๋•Œ Naming์„ ์–ด๋–ค ์ „๋žต์œผ๋กœ ๋‹ค๋ฃจ๋Š” ์ง€๋ฅผ ์„ค์ •ํ•œ๋‹ค. ๊ฐ’์„ ๋”ฐ๋กœ ์ง€์ •ํ•˜์ง€ ์•Š์œผ๋ฉด ์ „์ฒด ์†Œ๋ฌธ์ž + Under Score๋กœ ๋‹จ์–ด๊ฐ€ ์ด์–ด์ง€๋Š” Snake-case๊ฐ€ ์ ์šฉ๋˜๋ฉฐ ์ด๋•Œ ์‚ฌ์šฉ๋˜๋Š” ๊ตฌํ˜„์ฒด๋Š” org.springframework.boot.orm.jpa.hibernate.SpringPhysicalNamingStrategy ์ด๋‹ค. org.hibernate.boot.model.naming.PhysicalNamingStrategyStadardImpl์€ Snake-case๋กœ ๋ณ€ํ™˜ ์—†์ด Namingํ•˜๊ฒŒ ๋œ๋‹ค.

JPA ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜ ์ฝ”๋“œ

import javax.persistence.*;
import java.util.List;

public class JpaMain {
  public static void main(String[] args) {
    //[์—”ํ‹ฐํ‹ฐ ๋งค๋‹ˆ์ € ํŒฉํ† ๋ฆฌ] - ์ƒ์„ฑ
    EntityManagerFactory emf = Persistence.createEntityManagerFactory("project name");
    
    //[์—”ํ‹ฐํ‹ฐ ๋งค๋‹ˆ์ €] - ์ƒ์„ฑ
    EntityManager em = emf.createEntityManager();

    //[ํŠธ๋žœ์žญ์…˜] - ํš๋“
    EntityTransaction tx = em.getTransaction();

    try {
        tx.begin();     //[ํŠธ๋žœ์žญ์…˜] - ์‹œ์ž‘
        logic(em);      //๋น„์ฆˆ๋‹ˆ์Šค ๋กœ์ง ์‹คํ–‰
        tx.commit();    //[ํŠธ๋žœ์žญ์…˜] - ์ปค๋ฐ‹
    } catch (Exception e) {
        tx.rollback();  //[ํŠธ๋žœ์žญ์…˜] - ๋กค๋ฐฑ
    } finally {
        em.close();     //[์—”ํ‹ฐํ‹ฐ ๋งค๋‹ˆ์ €] - ์ข…๋ฃŒ
    }
    emf.close();        //[์—”ํ‹ฐํ‹ฐ ๋งค๋‹ˆ์ € ํŒฉํ† ๋ฆฌ] - ์ข…๋ฃŒ
  }

  private static void logi(EntityManager em) {...}
}

์ฝ”๋“œ๋Š” ํฌ๊ฒŒ 3๊ฐ€์ง€๋กœ ๋‚˜๋‰˜์–ด ์žˆ๋‹ค.

  • ์—”ํ‹ฐํ‹ฐ ๋งค๋‹ˆ์ € ์„ค์ •
  • ํŠธ๋žœ์žญ์…˜ ๊ด€๋ฆฌ
  • ๋น„์ฆˆ๋‹ˆ์Šค ๋กœ์ง

์—”ํ‹ฐํ‹ฐ ๋งค๋‹ˆ์ € ์„ค์ •

  • ์—”ํ‹ฐํ‹ฐ ๋งค๋‹ˆ์ € ํŒฉํ† ๋ฆฌ ์ƒ์„ฑ spring boot๋ผ๋ฉด application.yml์˜ ์„ค์ • ์ •๋ณด๋ฅผ ํ†ตํ•ด ์—”ํ‹ฐํ‹ฐ ๋งค๋‹ˆ์ € ํŒฉํ† ๋ฆฌ๋ฅผ ์ƒ์„ฑํ•œ๋‹ค. ์„ค์ • ์ •๋ณด๋ฅผ ํ†ตํ•ด JPA๋ฅผ ๋™์ž‘์‹œํ‚ค๊ธฐ ์œ„ํ•œ ๊ธฐ๋ฐ˜ ๊ฐ์ฒด๋ฅผ ๋งŒ๋“ค๊ณ  JPA ๊ตฌํ˜„์ฒด ๋”ฐ๋ผ์„œ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์ปค๋„ฅ์…˜ ํ’€๋„ ์ƒ์„ฑํ•˜๋ฏ€๋กœ ์—”ํ‹ฐํ‹ฐ ๋งค๋‹ˆ์ € ํŒฉํ† ๋ฆฌ๋ฅผ ์ƒ์„ฑํ•˜๋Š” ๋น„์šฉ์ด ์•„์ฃผ ํฌ๋‹ค. ๋”ฐ๋ผ์„œย ์—”ํ‹ฐํ‹ฐ ๋งค๋‹ˆ์ € ํŒฉํ† ๋ฆฌ๋Š” ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ์ „์ฒด์—์„œ ํ•œ ๋ฒˆ๋งŒ ์ƒ์„ฑํ•˜๊ณ  ๊ณต์œ ํ•ด์„œ ์‚ฌ์šฉํ•ด์•ผ ํ•œ๋‹ค.

  • ์—”ํ‹ฐํ‹ฐ ๋งค๋‹ˆ์ € ์ƒ์„ฑ ์—”ํ‹ฐํ‹ฐ ๋งค๋‹ˆ์ € ํŒฉํ† ๋ฆฌ์—์„œ ์—”ํ‹ฐํ‹ฐ ๋งค๋‹ˆ์ €๋ฅผ ์ƒ์„ฑํ•œ๋‹ค. ์—”ํ‹ฐํ‹ฐ ๋งค๋‹ˆ์ €๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์—”ํ‹ฐํ‹ฐ๋ฅผ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์— CRUDํ•  ์ˆ˜ ์žˆ๋‹ค. ์—”ํ‹ฐํ‹ฐ ๋งค๋‹ˆ์ €๋Š” ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์ปค๋„ฅ์…˜๊ณผ ๋ฐ€์ ‘ํ•œ ๊ด€๊ณ„๊ฐ€ ์žˆ์œผ๋ฏ€๋กœ ์Šค๋ ˆ๋“œ ๊ฐ„์— ๊ณต์œ ํ•˜๊ฑฐ๋‚˜ ์žฌ์‚ฌ์šฉํ•˜๋ฉด ์•ˆ ๋œ๋‹ค.

  • ์ข…๋ฃŒ ์‚ฌ์šฉ์ด ๋๋‚œ ์—”ํ‹ฐํ‹ฐ ๋งค๋‹ˆ์ €์™€ ์—”ํ‹ฐํ‹ฐ ๋งค๋‹ˆ์ € ํŒฉํ† ๋ฆฌ๋Š” ๋ฐ˜๋“œ์‹œ ์ข…๋ฃŒํ•ด์•ผ ํ•œ๋‹ค.

ํŠธ๋žœ์žญ์…˜ ๊ด€๋ฆฌ

JPA๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ฐ์ดํ„ฐ๋ฅผ ๋ณ€๊ฒฝํ•˜๋ ค๋ฉด ํ•ญ์ƒ ํŠธ๋žœ์žญ์…˜ ์•ˆ์—์„œ ์ด๋ค„์ ธ์•ผ ํ•œ๋‹ค. ์—”ํ‹ฐํ‹ฐ ๋ฉ”๋‹ˆ์ €์—์„œ ํŠธ๋žœ์žญ์…˜ API๋ฅผ ๋ฐ›์•„์™€์„œ ์‚ฌ์šฉํ•œ๋‹ค.

EntityTransaction tx = em.getTransaction(); //ํŠธ๋žœ์žญ์…˜ API

  try {
    tx.begin();     //ํŠธ๋žœ์žญ์…˜ ์‹œ์ž‘
    logic(em);      //๋น„์ฆˆ๋‹ˆ์Šค ๋กœ์ง ์‹คํ–‰
    tx.commit();    //ํŠธ๋žœ์žญ์…˜ ์ปค๋ฐ‹
  } catch (Exception e) {
    tx.rollback();  //์˜ˆ์™ธ ๋ฐœ์ƒ ์‹œ ํŠธ๋žœ์žญ์…˜ ๋กค๋ฐฑ
  }

๋น„์ฆˆ๋‹ˆ์Šค ๋กœ์ง

private static void logic(EntityManager em) {
  String id = "id";
  Member member = new Member();
  member.setId(id);
  member.setUsername("์žฌ๋‘");
  member.setAge(31);

  //๋“ฑ๋ก
  em.persist(member);

  //์ˆ˜์ •
  member.setAge(20);

  //ํ•œ ๊ฑด ์กฐํšŒ
  Member findMember = em.find(Member.class, id);
  System.out.println("name = " + member.getUserName() + " age = " + memger.getAge());

  //๋ชฉ๋ก ์กฐํšŒ
  String sql = "select m from Member m";
  List<Member> memberList = em.createQuery(sql, Member.class).getResultList();
  System.out.println("member size = " + memberList.size());

  //์‚ญ์ œ
  em.remove(member);
}
  • ๋“ฑ๋ก ํšŒ์› ์—”ํ‹ฐํ‹ฐ ์ƒ์„ฑ ํ›„ย em.persist(member);ย ๋ฅผ ์‹คํ–‰ํ•˜์—ฌ ์—”ํ‹ฐํ‹ฐ๋ฅผ ์ €์žฅํ•œ๋‹ค. JPA๋Š” ํšŒ์› ์—”ํ‹ฐํ‹ฐ ๋งคํ•‘ ์ •๋ณด(์–ด๋…ธํ…Œ์ด์…˜)์„ ๋ถ„์„ํ•ด (1์ฐจ ์บ์‹œ์— ์—†๋‹ค๋ฉด) ์•„๋ž˜ SQL์„ ์ƒ์„ฑํ•˜์—ฌ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์— ์ „๋‹ฌํ•œ๋‹ค.

    INSERT INTO MEMBER (ID, NAME, AGE) VALUES ('id', '์žฌ๋‘', '31');
  • ์ˆ˜์ • JPA๋Š” ์–ด๋–ค ์—”ํ‹ฐํ‹ฐ๊ฐ€ ๋ณ€๊ฒฝ๋˜์—ˆ๋Š”์ง€ ์ถ”์ ํ•˜๋Š” ๊ธฐ๋Šฅ์ด ์žˆ์–ด, ๊ธฐ์กด์— ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์— ์กด์žฌํ•˜๋Š” ๊ฐ’์„ ๋ณ€๊ฒฝํ•˜๋Š” ๊ฒƒ์ด๋ผ๋ฉด ์•„๋ž˜์™€ ๊ฐ™์€ ์ˆ˜์ • SQL์„ ์ƒ์„ฑํ•˜์—ฌ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์— ์ „๋‹ฌํ•œ๋‹ค.

    UPDATE MEMBER SET AGE=20 WHERE ID = 'id';
  • ์‚ญ์ œ ์—”ํ‹ฐํ‹ฐ๋ฅผ ์‚ญ์ œํ•˜๋ ค๋ฉด ์—”ํ‹ฐํ‹ฐ ๋งค๋‹ˆ์ €์˜ remove() ๋ฉ”์†Œ๋“œ๋ฅผ ์‹คํ–‰ํ•˜๋ฉด ๋œ๋‹ค. ๊ทธ๋Ÿฌ๋ฉด JPA๋Š” ์‚ญ์ œ SQL์„ ์ƒ์„ฑํ•˜์—ฌ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์— ์ „๋‹ฌํ•œ๋‹ค.

    DELETE FROM MEMGER WHERE ID = 'id';
  • ํ•œ ๊ฑด ์กฐํšŒ find() ๋ฉ”์†Œ๋“œ๋ฅผ ์‹คํ–‰ํ•˜๋ฉด JPA์—์„œ๋Š” ์กฐํšŒํ•  ์—”ํ‹ฐํ‹ฐ ํƒ€์ž…๊ณผ @Id ์–ด๋…ธํ…Œ์ด์…˜์œผ๋กœ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ํ…Œ์ด๋ธ”์˜ ๊ธฐ๋ณธ ํ‚ค์™€ ๋งคํ•‘ํ•œ ์‹๋ณ„์ž ๊ฐ’์„ ์ฐพ์•„ (1์ฐจ ์บ์‹œ์— ํ•ด๋‹น ๊ฐ’์ด ์—†๋‹ค๋ฉด) ํ•˜๋‚˜์˜ ์—”ํ‹ฐํ‹ฐ๋ฅผ ์กฐํšŒํ•˜๋Š” SQL์„ ์ƒ์„ฑํ•˜์—ฌ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์— ์ „๋‹ฌํ•œ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์กฐํšŒํ•œ ๊ฒฐ๊ณผ ๊ฐ’์œผ๋กœ ์—”ํ‹ฐํ‹ฐ๋ฅผ ์ƒ์„ฑํ•˜์—ฌ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

  • ๋ชฉ๋ก ์กฐํšŒ JPA๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๊ฐœ๋ฐœ์ž๋Š” ์—”ํ‹ฐํ‹ฐ ๊ฐ์ฒด ์ค‘์‹ฌ์œผ๋กœ ๊ฐœ๋ฐœํ•˜๊ณ  ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์— ๋Œ€ํ•œ ์ฒ˜๋ฆฌ๋Š” JPA์— ๋งก๊ฒจ์•ผ ํ•œ๋‹ค. ์œ„ ๊ธฐ๋Šฅ๋“ค์€ ๋ชจ๋‘ SQL์„ ์‚ฌ์šฉํ•˜์ง€ ์•Š์•˜์œผ๋‚˜, ๋ชฉ๋ก ์กฐํšŒ์™€ ๊ฐ™์€ ๊ฒ€์ƒ‰ ์ฟผ๋ฆฌ๋Š” ๋ชจ๋“  ์—”ํ‹ฐํ‹ฐ ๊ฐ์ฒด๋ฅผ ๋Œ€์ƒ์œผ๋กœ ๊ฒ€์ƒ‰ํ•ด์•ผ ํ•˜๋Š”๋ฐ SQL์€ ํ…Œ์ด๋ธ”์„ ๋Œ€์ƒ์œผ๋กœ ์ฟผ๋ฆฌ๋ฅผ ํ•œ๋‹ค. ์ด ๋•Œ JPQL์„ ์‚ฌ์šฉํ•ด์•ผ ํ•œ๋‹ค.

  • JPQL(Java Persistence Query Language) SQL์„ ์ถ”์ƒํ™”ํ•œ ๊ฐ์ฒด์ง€ํ–ฅ ์ฟผ๋ฆฌ ์–ธ์–ด๋กœ์จ ์—”ํ‹ฐํ‹ฐ ๊ฐ์ฒด๋ฅผ ๋Œ€์ƒ์œผ๋กœ ์ฟผ๋ฆฌํ•œ๋‹ค. ์ฆ‰, ํด๋ž˜์Šค์™€ ํ•„๋“œ๋ฅผ ๋Œ€์ƒ์œผ๋กœ ์ฟผ๋ฆฌํ•œ๋‹ค.

๐Ÿ“— ์ฐธ๊ณ 

  • ์ž๋ฐ” ORM ํ‘œ์ค€ JPA ํ”„๋กœ๊ทธ๋ž˜๋ฐ, ๊น€์˜ํ•œ