๐ŸŒป JAVA/์ž๋ฐ” ORM ํ‘œ์ค€ JPA ํ”„๋กœ๊ทธ๋ž˜๋ฐ

8-1) ํ”„๋ก์‹œ์™€ ์—ฐ๊ด€๊ด€๊ณ„ ๊ด€๋ฆฌ : ํ”„๋ก์‹œ

iseunghan 2020. 11. 6. 17:43
๋ฐ˜์‘ํ˜•

 

์ž๋ฐ” ORM ํ‘œ์ค€ JPA ํ”„๋กœ๊ทธ๋ž˜๋ฐ - ๊ธฐ๋ณธํŽธ์„ ๊ณต๋ถ€ํ•˜๋ฉฐ ์ •๋ฆฌํ•œ ๋‚ด์šฉ์ž…๋‹ˆ๋‹ค.

 

์ž๋ฐ” ORM ํ‘œ์ค€ JPA ํ”„๋กœ๊ทธ๋ž˜๋ฐ - ๊ธฐ๋ณธํŽธ - ์ธํ”„๋Ÿฐ

JPA๋ฅผ ์ฒ˜์Œ ์ ‘ํ•˜๊ฑฐ๋‚˜, ์‹ค๋ฌด์—์„œ JPA๋ฅผ ์‚ฌ์šฉํ•˜์ง€๋งŒ ๊ธฐ๋ณธ ์ด๋ก ์ด ๋ถ€์กฑํ•˜์‹  ๋ถ„๋“ค์ด JPA์˜ ๊ธฐ๋ณธ ์ด๋ก ์„ ํƒ„ํƒ„ํ•˜๊ฒŒ ํ•™์Šตํ•ด์„œ ์ดˆ๋ณด์ž๋„ ์‹ค๋ฌด์—์„œ ์ž์‹ ์žˆ๊ฒŒ JPA๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ดˆ๊ธ‰ ์›น ๊ฐœ๋ฐœ ํ”„๋กœ๊ทธ๏ฟฝ

www.inflearn.com

 

โœ… ํ”„๋ก์‹œ

  • ํ”„๋ก์‹œ ํŠน์ง•
    • ์‹ค์ œ ํด๋ž˜์Šค๋ฅผ ์ƒ์† ๋ฐ›์•„์„œ ๋งŒ๋“ค์–ด์กŒ๋‹ค.
    • ์‹ค์ œ ํด๋ž˜์Šค์™€ ๊ฒ‰ ๋ชจ์–‘์ด ๊ฐ™๋‹ค
    • ์‹ค์ œ ๊ฐ์ฒด์˜ ์ฐธ์กฐ(target)๋ฅผ ๋ณด๊ด€
    • ํ”„๋ก์‹œ ๊ฐ์ฒด๋ฅผ ํ˜ธ์ถœํ•˜๋ฉด ํ”„๋ก์‹œ ๊ฐ์ฒด๋Š” ์‹ค์ œ ๊ฐ์ฒด์˜ ๋ฉ”์†Œ๋“œ๋ฅผ ํ˜ธ์ถœํ•œ๋‹ค.

  • em.find() : DB๋ฅผ ํ†ตํ•ด์„œ ์‹ค์ œ ์—”ํ‹ฐํ‹ฐ ๊ฐ์ฒด ์กฐํšŒ!
  • em.getReference() : DB ์กฐํšŒ๋ฅผ ๋ฏธ๋ฃจ๋Š” ๊ฐ€์งœ(ํ”„๋ก์‹œ) ์—”ํ‹ฐํ‹ฐ ๊ฐ์ฒด ์กฐํšŒ

ํ”„๋ก์‹œ ๊ฐ์ฒด์—๋Š” ์‹ค์ œ ์—”ํ‹ฐํ‹ฐ๋ฅผ ์ฐธ์กฐํ•˜๋Š” target์„ ๊ฐ€์ง€๊ณ  ์žˆ๋‹ค!

 

 

ํ”„๋ก์‹œ ๊ฐ์ฒด์˜ ์ดˆ๊ธฐํ™”

Member m1 = em.getReference(Member.class, member.getId()); // ์ด๋•Œ๋Š” ์ฟผ๋ฆฌ๊ฐ€ ๋‚˜๊ฐ€์ง€ ์•Š๊ฒŒ ๋œ๋‹ค.
m1.getName(); // **์‹ค์ œ ๊ฐ์ฒด๋ฅผ ์ฐธ์กฐํ• ๋•Œ! ์ฟผ๋ฆฌ๊ฐ€ ๋‚˜๊ฐ„๋‹ค.

 

ํ”„๋ก์‹œ ๊ฐ์ฒด์˜ ์ดˆ๊ธฐํ™” ๊ณผ์ •

  1.  m1.getName();
  2. target ์ด null ์ด๋ฏ€๋กœ ์˜์†์„ฑ ์ปจํ…์ŠคํŠธ์— ์ดˆ๊ธฐํ™” ์š”์ฒญ์„ ํ•œ๋‹ค.
  3. ์˜์†์„ฑ ์ปจํ…์ŠคํŠธ์— 1์ฐจ ์บ์‹œ์— ์กฐํšŒํ•ด์„œ ์—†์œผ๋‹ˆ๊นŒ DB์—์„œ ์กฐํšŒ๋ฅผ ํ•œ๋‹ค.
  4. ์‹ค์ œ Entity๊ฐ€ ์ƒ์„ฑ์ด ๋˜๊ณ ..
  5. target์— Member๊ฐ€ ๋‹ด๊ธฐ๊ฒŒ ๋˜๊ณ , target.getName() ์ด ๊ฐ€๋Šฅํ•˜๊ฒŒ ๋œ๋‹ค!

 


Proxy ๊ฐ์ฒด์˜ ์ดˆ๊ธฐํ™” ๊ณผ์ • :  em.getReference(T class, id(key) );

public class JpaMain {
	// EntityManagerFactory, EntityManager, EntityTransaction ์ดˆ๊ธฐํ™” ์ƒ๋žต

	try {
    	Member member1 = new Member();
        member1.setUsername("hello");
        em.persist(member1);
        
        em.flush();
        em.clear(); // ์˜์†์„ฑ ์ปจํ…์ŠคํŠธ์˜ 1์ฐจ์บ์‹œ๋ฅผ ๋น„์šด๋‹ค.
        
        Member findMember = em.getReference(Member.class, member1.getId()); // (๊ฐ€์งœ ๊ฐ์ฒด์ด๊ธฐ ๋•Œ๋ฌธ!) ์ด ์‹œ์ ์—์„œ๋Š” ์ฟผ๋ฆฌ๊ฐ€ ์•ˆ๋‚˜๊ฐ„๋‹ค.
        System.out.println("findMember : " + findMember.getClass()); 
        System.out.println("findMember.id : " + findMember.getId()); // Id๋ฅผ ์กฐํšŒํ•˜๋Š”๊ฒƒ๋„ ์ฟผ๋ฆฌ๊ฐ€ ์•ˆ๋‚˜๊ฐ„๋‹ค. ์™œ? 
        System.out.println("findMember.name : " + findMember.getUsername());
        System.out.println("findMember.name : " + findMember.getUsername());
        
    }
}
findMember : class chapter08_ํ”„๋ก์‹œ_์—ฐ๊ด€๊ด€๊ณ„๊ด€๋ฆฌ.Member$HibernateProxy$waCRsb9J
findMember.id : 1
findMember.name : hello1
findMember.name : hello1

em.getReference()๋กœ ์ฐพ์€ member๋ฅผ getClass()๋ฅผ ์ถœ๋ ฅํ•ด๋ณด๋‹ˆ proxy๊ฐ€ ๋‚˜์˜ค๋Š”๊ฑธ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.

 

ํ”„๋ก์‹œ์˜ ํŠน์ง•

  • ํ”„๋ก์‹œ ๊ฐ์ฒด๋Š” ์ฒ˜์Œ ์‚ฌ์šฉํ•  ๋•Œ ํ•œ ๋ฒˆ๋งŒ ์ดˆ๊ธฐํ™”
  • ํ”„๋ก์‹œ ๊ฐ์ฒด๋Š” ์›๋ณธ ์—”ํ‹ฐํ‹ฐ๋ฅผ ์ƒ์† ๋ฐ›์•˜๊ธฐ ๋•Œ๋ฌธ์—, ( == ๋น„๊ต ์•ˆ๋จ! instance of ์‚ฌ์šฉ!! )
  • ์˜์†์„ฑ ์ปจํ…์ŠคํŠธ์— ์ด๋ฏธ ์žˆ๋Š” ์—”ํ‹ฐํ‹ฐ๋Š” em.getReference() ํ•ด๋„ ์‹ค์ œ ์—”ํ‹ฐํ‹ฐ ๋ฐ˜ํ™˜!
public class JpaMain {
	// EntityManagerFactory, EntityManager, EntityTransaction ์ดˆ๊ธฐํ™” ์ƒ๋žต

	try {
    	Member member1 = new Member();
        member1.setUsername("hello");
        em.persist(member1);
        
        em.flush();
        em.clear(); // ์˜์†์„ฑ ์ปจํ…์ŠคํŠธ์˜ 1์ฐจ์บ์‹œ๋ฅผ ๋น„์šด๋‹ค.
        
        Member findMember = em.find(Member.class, member1.getId()); // ์ฟผ๋ฆฌ ๋‚˜๊ฐ.
        Member proxyMember1 = em.getReference(Member.class, member1.getId()); // // proxy ๊ฐ์ฒด ์š”์ฒญ!
        System.out.println("findMember : " + findMember.getClass()); // ์‹ค์ œ ์—”ํ‹ฐํ‹ฐ ๋‚˜์˜ด
        System.out.println("proxyMember : " + proxyMember1.getClass()); // proxy๊ฐ์ฒด๋ฅผ ์š”์ฒญํ–ˆ์ง€๋งŒ, em.find()์— ์˜ํ•ด ์ด๋ฏธ ์˜์†์„ฑ ์ปจํ…์ŠคํŠธ์— ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ์‹ค์ œ ์—”ํ‹ฐํ‹ฐ๋ฅผ ๋ฐ˜ํ™˜์‹œํ‚จ๋‹ค.
    }
}

 

๋ฐ˜์‘ํ˜•