JPA

JPA(Java Persistence API) Jdbc Template์„ ์ด์šฉํ•ด๋„ SQL ์ฟผ๋ฆฌ๋ฌธ์€ ๊ฐœ๋ฐœ์ž๊ฐ€ ์ง์ ‘ ์ž‘์„ฑํ•ด์•ผํ–ˆ์ง€๋งŒ, JPA๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด SQL์ฟผ๋ฆฌ๋„ JPA๊ฐ€ ์ž๋™์œผ๋กœ ์ฒ˜๋ฆฌํ•ด์ค€๋‹ค! ์ด๋กœ์จ ๊ฐœ๋ฐœ์ƒ์‚ฐ์„ฑ์„ ํฌ๊ฒŒ ๋†’์ผ ์ˆ˜ ์žˆ๋‹ค!

๋‹จ์ˆœํžˆ SQL๋ฌธ์„ ์ž‘์„ฑํ•ด์ฃผ๋Š” ๊ฒƒ์„ ๋„˜์–ด SQL๊ณผ ๋ฐ์ดํ„ฐ ์ค‘์‹ฌ์ด์ด ์•„๋‹ˆ๋ผ ๊ฐ์ฒด ์ค‘์‹ฌ(๊ฐ์ฒด ์ค‘์‹ฌ์˜ ์„ค๊ณ„)์œผ๋กœ ์ƒ๊ฐํ•  ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ํŒจ๋Ÿฌ๋‹ค์ž„์„ ์ „ํ™˜ํ•  ์ˆ˜ ์žˆ๋‹ค!

์Šคํ”„๋ง์€ ์—„์ฒญ๋‚˜๊ฒŒ ํฐ ๊ธฐ์ˆ ์ด๊ณ , JPA๋„ ์Šคํ”„๋ง๋งŒํผ ๋„“์ด์™€ ๊นŠ์ด๊ฐ€ ์žˆ๋‹ค. ์‹ค์ œ๋กœ ์Šคํ”„๋ง ํ”„๋ ˆ์ž„์›Œํฌ์—์„œ ๊ด€๊ณ„ํ˜• ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ๊ด€๋ จ๋œ ์˜ˆ์ œ๋“ค์€ JPA๊ฐ€ ๊ธฐ๋ณธ์œผ๋กœ ๊น”๊ณ  ๋“ค์–ด๊ฐ€์žˆ๋‹ค.

JPA๋Š” Java ์ง„์˜์˜ ํ‘œ์ค€ ์ธํ„ฐํŽ˜์ด์Šค์ด๊ณ , ๊ตฌํ˜„์€ hibernate๋‚˜ eclipse ๊ฐ™์€ ์—ฌ๋Ÿฌ ์—…์ฒด๋“ค์ด ํ•œ๋‹ค. ORM(Object Relational Mapping) : ๊ฐ์ฒด์™€ ๊ด€๊ณ„ํ˜• DB๋ฅผ ๋งตํ•‘ํ•œ๋‹ค๋Š” ๋œป์ด๋‹ค! DB์™€์˜ ๋งตํ•‘์€ ์–ด๋…ธํ…Œ์ด์…˜์œผ๋กœ ํ•  ์ˆ˜ ์žˆ๋‹ค!

JPA ์‚ฌ์šฉ๋ฐฉ๋ฒ• 1. application.properties : ์•„๋ž˜์ฒ˜๋Ÿผ ์ถ”๊ฐ€ํ•ด์ค€๋‹ค! JPA๋Š” ํšŒ์› ๊ฐ์ฒด๋ฅผ ๋ณด๊ณ  ์ž๋™์œผ๋กœ ํ…Œ์ด๋ธ”์„ ์ƒ์„ฑํ•œ๋‹ค. ํ˜„์žฌ ํ…Œ์ด๋ธ”์ด ๋งŒ๋“ค์–ด์ ธ์žˆ๊ณ , ์ด๊ฒƒ์„ ์‚ฌ์šฉํ•  ๊ฒƒ์ด๊ธฐ ๋•Œ๋ฌธ์— ํ…Œ์ด๋ธ” ์ž๋™ ์ƒ์„ฑ ๊ธฐ๋Šฅ์€ 'none'์œผ๋กœ ํ•ด์ค€๋‹ค.

spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=none

2. build.gradle : jpa๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์ƒ์„ฑ => 1์—์„œ ์…‹ํŒ…ํ•œ ์ •๋ณด์™€ DB ์ปค๋„ฅ์…˜ ์ •๋ณด๋ฅผ ํ•ฉํ•ด์„œ ์Šคํ”„๋ง๋ถ€ํŠธ๊ฐ€ ์ž๋™์œผ๋กœ EntityManager์ƒ์„ฑํ•ด์ค€๋‹ค! JPA๋ฅผ ์“ฐ๋ ค๋ฉด EntityManager๋ฅผ ์ฃผ์ž…๋ฐ›์•„์•ผํ•œ๋‹ค. JPA๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” EntityManager๊ฐ€ ์žˆ์–ด์•ผํ•œ๋‹ค!

private final EntityManager em;//JPA๋Š” ์ด ๋งค๋‹ˆ์ €๋กœ  ๋™์ž‘ํ•œ๋‹ค!

    public JpaMemberRepository(EntityManager em) {
        this.em = em;
    }

JPA ์‚ฌ์šฉํ•  ๋•Œ ์ฃผ์˜ํ•  ์ 

ํ•ญ์ƒ @Transactional์ด ์žˆ์–ด์•ผํ•œ๋‹ค! MemberService ํด๋ž˜์Šค์—์„œ ํด๋ž˜์Šค ์‹œ์ž‘์ „์— ์ถ”๊ฐ€ํ•ด์ค€๋‹ค!

JpaMemberRepository.java ํŠน์ง• : PK(Primary Key)์ธ Id๊ฐ’์„ ๊ฐ€์ ธ์˜ฌ ๋•Œ๋Š” ์ƒ๊ด€์—†์ง€๋งŒ, PK๊ฐ€ ์•„๋‹Œ ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•ด์„œ๋Š” JPQL์„ ์‚ฌ์šฉํ•œ๋‹ค. ์ด JPQL์€ ํ…Œ์ด๋ธ”์„ ๋Œ€์ƒ์œผ๋กœ ํ•˜๋Š” ์ฟผ๋ฆฌ๊ฐ€ ์•„๋‹ˆ๋ผ ๊ฐ์ฒด(Member Entity)๋ฅผ ๋Œ€์ƒ์œผ๋กœ ์ฟผ๋ฆฌ๋ฅผ ๋งŒ๋“ ๋‹ค!

์˜ˆ๋ฅผ ๋“ค์–ด findAll()๋ฉ”์„œ๋“œ๋ฅผ ๊ตฌํ˜„ํ•  ๋•Œ select๋ฌธ์œผ๋กœ ์ž‘์„ฑํ•˜๋ฉด select m.id(m.name)์œผ๋กœ ํ•˜์ง€๋งŒ ์•„๋ž˜์ฒ˜๋Ÿผ select m์œผ๋กœ Member ์—”ํ‹ฐํ‹ฐ ๊ทธ ์ž์ฒด๋ฅผ ๊ฐ€์ ธ์˜ฌ ์ˆ˜ ์žˆ๋‹ค. ๊ฐ€์ ธ์˜ค๋Š” ํƒ€์ž…์€ Member.class

@Override
public List<Member> findAll() {
  return em.createQuery("select m from Member m",Member.class)
        .getResultList();
}
package hello.hellospring.repository;

import hello.hellospring.domain.Member;

import javax.persistence.EntityManager;
import java.util.List;
import java.util.Optional;

public class JpaMemberRepository implements MemberRepository{
    private final EntityManager em;//JPA๋Š” ์ด ๋งค๋‹ˆ์ €๋กœ  ๋™์ž‘ํ•œ๋‹ค!

    public JpaMemberRepository(EntityManager em) {
        this.em = em;
    }

    @Override
    public Member save(Member member) {
        em.persist(member);
        return member;
    }

    @Override
    public Optional<Member> findById(Long id) {
        Member member = em.find(Member.class,id);
        return Optional.ofNullable(member);
    }

    @Override
    public Optional<Member> findByName(String name) {
        List<Member> result = em.createQuery("select m from Member m where m.name = :name", Member.class)
                .setParameter("name",name)
                .getResultList();
        return result.stream().findAny();
    }

    @Override
    public List<Member> findAll() {
        return em.createQuery("select m from Member m",Member.class)
                .getResultList();
    }

}

Last updated