public class JpaMain {

    public static void main(String[] args) {

        System.out.println(1111);

        EntityManagerFactory emf = Persistence.createEntityManagerFactory("hello");
        EntityManager em = emf.createEntityManager();

        EntityTransaction tx = em.getTransaction();
        tx.begin();

        try {
            Member findMember = em.find(Member.class, 1L);
            findMember.setName("member3");
            
            tx.commit();
        } catch (Exception e) {
            tx.rollback();
        } finally {
            em.close();
        }
        emf.close();
    }
}
  • em.persist(member)๋ฅผ ํ•˜์ง€ ์•Š์•˜์Œ์—๋„update์ฟผ๋ฆฌ๊ฐ€ ๋‚˜๊ฐ„๋‹ค.
  • ๊ทธ ์ด์œ ๋Š” jpa๋ฅผ ํ†ตํ•ด์„œ entity๋ฅผ ๊ฐ€์ ธ์˜ค๋ฉด jpa๊ฐ€ ๊ด€๋ฆฌํ•œ๋‹ค. ๊ทธ๋ ‡๊ธฐ ๋•Œ๋ฌธ์—, ํŠธ๋žœ์žญ์…˜ ์ปค๋ฐ‹ ์‹œ์ ์— jpa๊ฐ€ ๋ณ€๊ฒฝ์‚ฌํ•ญ์ด ์žˆ๋Š”์ง€๋ฅผ ํ™•์ธ(๋ณ€๊ฒฝ ๊ฐ์ง€)ํ•˜๊ณ  update ์ฟผ๋ฆฌ๋ฅผ ์ง„ํ–‰ํ•˜๋Š” ๊ฒƒ์ด๋‹ค.
  • JPA์—์„œ ๋ฐ์ดํ„ฐ ๋ณ€๊ฒฝ์€ ํŠธ๋žœ์žญ์…˜ ์•ˆ์—์„œ ์‹คํ–‰

1. ์กฐํšŒ๋ฐฉ๋ฒ•

EntityManager.find()

2. ๋ณต์žกํ•œ ์กฐํšŒ๋Š”???

JPQL : ๊ฐ์ฒด ์ง€ํ–ฅ ์ฟผ๋ฆฌ ์–ธ์–ด

  • ์—”ํ‹ฐํ‹ฐ ๊ฐ์ฒด๋ฅผ ๋Œ€์ƒ์œผ๋กœ ์ฟผ๋ฆฌ๋ฅผ ์ž‘์„ฑ(ํ…Œ์ด๋ธ”์„ ๋Œ€์ƒ์œผ๋กœ ํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹˜)
    List<Member> resultList = em.createQuery("select m from Member as m", Member.class).getResultList();
    for (Member member : resultList) {
      System.out.println("member.name = " + member.getName());
    }
    

Tags:

Categories:

Date:

Leave a comment