๐ ์ค๋์ ํ์ต ํค์๋
- ์์์ฑ ์ปจํ ์คํธ
๐ฅ ์์์ฑ ์ปจํ ์คํธ๋?
- Persistence๋ฅผ ํ๊ธ๋ก ๋ฒ์ญํ๋ฉด ์์์ฑ, ์ง์์ฑ์ด๋ผ๋ ๋ป์ ๋๋ค.
Persistence๋ฅผ ๊ฐ์ฒด์ ๊ด์ ์ผ๋ก ํด์ํด ๋ณด์๋ฉด ๊ฐ์ฒด๊ฐ ์๋ช (๊ฐ์ฒด๊ฐ ์ ์ง๋๋ ์๊ฐ)์ด๋ ๊ณต๊ฐ(๊ฐ์ฒด์ ์์น)์ ์์ ๋กญ๊ฒ ์ ์งํ๊ณ ์ด๋ํ ์ ์๋ ๊ฐ์ฒด์ ์ฑ์ง์ ์๋ฏธํฉ๋๋ค.
์์์ฑ ์ปจํ ์คํธ๋ JPA๋ฅผ ์ดํดํ๋๋ฐ ๊ฐ์ฅ ์ค์ํ ์ฉ์ด์ด๋ค.
- ์์์ฑ ์ปจํ ์คํธ๋ ๋ ผ๋ฆฌ์ ์ธ ๊ฐ๋
- ๋์ ๋ณด์ด์ง ์์
- ์ํฐํฐ ๋งค๋์ ๋ฅผ ํตํด ์์์ฑ ์ปจํ ์คํธ์ ์ ๊ทผ
์์์ฑ ์ปจํ ์คํธ๋ฅผ ์ข ๋ ์ฝ๊ฒ ํํํด ๋ณด์๋ฉด Entity ๊ฐ์ฒด๋ฅผ ํจ์จ์ ์ผ๋ก ์ฝ๊ฒ ๊ด๋ฆฌํ๊ธฐ ์ํด ๋ง๋ค์ด์ง ๊ณต๊ฐ์ ๋๋ค.
- ๋น์์(new/transient) : ์์์ฑ ์ปจํ ์คํธ์ ์ ํ ๊ด๊ณ๊ฐ ์๋ ์๋ก์ด ์ํ
- ์์(managed) : ์์์ฑ ์ปจํ ์คํธ์ ๊ด๋ฆฌ๋๋ ์ํ
- ์ค์์(detached) : ์์์ฑ ์ปจํ ์คํธ์ ์ ์ฅ๋์๋ค๊ฐ ๋ถ๋ฆฌ๋ ์ํ
- ์ญ์ (remove) : ์ญ์ ๋ ์ํ
๐ฅ EntityManager
- ์์์ฑ ์ปจํ ์คํธ์ ์ ๊ทผํ์ฌ Entity ๊ฐ์ฒด๋ค์ ์กฐ์ํ๊ธฐ ์ํด์๋ EntityManager ๊ฐ ํ์ํฉ๋๋ค.
- EntityManager ๋ ์ด๋ฆ ๊ทธ๋๋ก Entity ๋ฅผ ๊ด๋ฆฌํ๋ ๊ด๋ฆฌ์์ ๋๋ค.
- ๊ฐ๋ฐ์๋ค์ EntityManager๋ฅผ ์ฌ์ฉํด์ Entity๋ฅผ ์ ์ฅํ๊ณ ์กฐํํ๊ณ ์์ ํ๊ณ ์ญ์ ํ ์ ์์ต๋๋ค.
- EntityManager๋ EntityManagerFactory๋ฅผ ํตํด ์์ฑํ์ฌ ์ฌ์ฉํ ์ ์์ต๋๋ค.
๐ฅ EntityManagerFactory
- EntityManagerFactory๋ ์ผ๋ฐ์ ์ผ๋ก DB ํ๋์ ํ๋๋ง ์์ฑ๋์ด ์ ํ๋ฆฌ์ผ์ด์ ์ด ๋์ํ๋ ๋์ ์ฌ์ฉ๋ฉ๋๋ค.
- EntityManagerFactory๋ฅผ ๋ง๋ค๊ธฐ ์ํด์๋ DB์ ๋ํ ์ ๋ณด๋ฅผ ์ ๋ฌํด์ผ ํฉ๋๋ค.
- ์ ๋ณด๋ฅผ ์ ๋ฌํ๊ธฐ ์ํด์๋ /resources/META-INF/ ์์น์ persistence.xml ํ์ผ์ ๋ง๋ค์ด ์ ๋ณด๋ฅผ ๋ฃ์ด๋๋ฉด ๋ฉ๋๋ค.
EntityManagerFactory emf = Persistence.createEntityManagerFactory("test");
EntityManager em = emf.createEntityManager();
- EntityManagerFactory emf = Persistence.createEntityManagerFactory("test");
- ํด๋น ์ฝ๋๋ฅผ ํธ์ถํ๋ฉด JPA๋ persistence.xml ์ ์ ๋ณด๋ฅผ ํ ๋๋ก EntityManagerFactory๋ฅผ ์์ฑํฉ๋๋ค.
- EntityManager em = emf.createEntityManager(); ์ฝ๋๋ฅผ ํธ์ถํ๋ฉด EntityManagerFactory๋ฅผ ์ฌ์ฉํ์ฌ EntityManager๋ฅผ ์์ฑํ ์ ์์ต๋๋ค.
๐ฅ ์์์ฑ ์ปจํ ์คํธ์ ์ด์
1. 1์ฐจ ์บ์
- ์์์ฑ ๋ด๋ถ์๋ 1์ฐจ ์บ์๊ฐ ์กด์ฌํฉ๋๋ค. ์์ ์ํ์ ์ํฐํฐ๋ฅผ ์ด๊ณณ์ ์ ์ฅํ๊ธฐ ๋๋ฌธ์ ์ํฐํฐ๋ฅผ ์กฐํํ์ ๋,
- 1์ฐจ ์บ์์ ์ํฐํฐ๊ฐ ์กด์ฌํ๋ค๋ฉด DB๋ฅผ ์ฐพ์๋ณด์ง ์์๋ ๋ฉ๋๋ค.
2. ์์ ์ํฐํฐ์ ๋์ผ์ฑ ๋ณด์ฅ
Member a = em.find(Member.class, "member1");
Member b = em.find(Member.class, "member1");
System.out.println(a==b) // true
3. ์ฐ๊ธฐ ์ง์ฐ
EntityManager em = emf.createEntityManager();
EntityTransaction transaction = em.getTransaction();
//์ํฐํฐ ๋งค๋์ ๋ ๋ฐ์ดํฐ ๋ณ๊ฒฝ์ ํธ๋์ญ์
์ ์์ํด์ผ ํฉ๋๋ค.
transaction.begin(); // transaction ์์
em.persist(memberA);
em.persist(memberB);
// ์ฌ๊ธฐ๊น์ง๋ INSERT ๋ฌธ์ ๋ฐ์ดํฐ๋ฒ ์ด์ค์ ๋ณด๋ด์ง ์์ต๋๋ค.
// ์ปค๋ฐํ๋ ์๊ฐ ๋ฐ์ดํฐ๋ฒ ์ด์ค์ INSERT ๋ฌธ์ ๋ณด๋ธ๋ค.
transaction.commit(); // ์ปค๋ฐ
4. Dirty Checking (๋ณ๊ฒฝ ๊ฐ์ง)
EntityManager em = emf.createEntityManager();
EntityTransaction transaction = em.getTransaction();
transaction.begin(); // transaction ์์
// ์์ ์ํฐํฐ ์กฐํ
Member memberA = em.find(Member.class, "memberA");
// ์์ ์ํฐํฐ ๋ฐ์ดํฐ ์์
memberA.setUsername("hi");
memberA.setAge(10);
transaction.commit(); // ์ปค๋ฐ
์์์ฑ ์ปจํ ์คํธ์์ ์ํฐํฐ๋ฅผ ์กฐํํด์ ํด๋น ์ํฐํฐ๋ฅผ ์์ ํ๋ค๊ณ ํ์ ๋,
์กฐํํ ์ํฐํฐ๋ฅผ ๋ค์ ์ ๋ฐ์ดํธํ๋ ์ฝ๋๊ฐ ์์ด์ผ ํ ๊ฒ ๊ฐ์ง๋ง, ๊ทธ๋ฐ ์ฝ๋๊ฐ ์์ด๋
์์์ฑ ์ปจํ ์คํธ๋ด์ ์ค๋ ์ท๊ณผ ์ํฐํฐ๋ฅผ ๋น๊ตํด ๋ณ๊ฒฝ๋ ์ํฐํฐ๊ฐ ์์ผ๋ฉด Update ์ฟผ๋ฆฌ๋ฅผ ์๋์ผ๋ก ์์ฑํฉ๋๋ค.
๐ ์ค๋์ ํ๊ณ
์์์ฑ ์ปจํ ์คํธ.. ๋ณต์ตํฉ์๋ค