μλ° ORM νμ€ JPA νλ‘κ·Έλλ° : κ΅λ³΄λ¬Έκ³
μλ° ORM νμ€ JPA νλ‘κ·Έλλ° - κΈ°λ³ΈνΈ : μΈνλ°
μ€μ Entity λμ DB μ‘°νλ₯Ό μ§μ°ν μ μλ κ°μ²΄λ₯Ό λ§νλ€.
proxy κ°μ²΄λ μ€μ Entity λ₯Ό μμ
λ°μμ λ§λ€μ΄μ§ κ°μ²΄μ΄λ€.
μ¬μ©μ μ
μ₯μμ Entity μΈμ§ Proxy κ°μ²΄μΈμ§ ꡬλΆνμ§ λͺ»νλ€.
Proxy κ°μ²΄λ μ€μ κ°μ²΄ μ°Έμ‘°λ₯Ό 보κ΄νλλ° μ²μ Proxy κ°μ²΄κ° μμ±λλ©΄ null
μ΄ ν λΉλλ€.
μ¬μ©μλ₯Ό λμ νμ¬ Entity μ Method νΈμΆνλ€.
SUMMARY
1. proxy κ°μ²΄λμ€μ Entity λ₯Ό μμ
λ°μμ λ§λ€μ΄μ§ κ°μ²΄
2. μ€μ κ°μ²΄λ₯Ό μ°Έμ‘°νμ¬ λ³΄κ΄νλ©° μ²μ μμ±μnull
ν λΉ
3. Entity μ method κ° νΈμΆλλ©΄ Proxy κ°μ²΄μ μ€μ κ°μ²΄μ μ°Έμ‘°λ₯Ό 보κ΄
4. μ¬μ©μλ₯Ό λμ νμ¬ μ€μ κ°μ²΄μ λ©μλλ₯Ό λμ νΈμΆ
- Proxy κ°μ²΄ μΆκ°
- μ¬μ©μκ° Entity Method νΈμΆνλ©΄ Proxy κ°μ²΄κ° Persistence Context μ μ΄κΈ°ν μμ²
- DB μμ Persistence Context 1μ°¨ cache λ‘ Entity μμ±
- μμ²ν method λ₯Ό Proxy κ°μ²΄κ° λμ μν
Entity μ Method νΈμΆ μ Proxy κ°μ²΄μ μ€μ κ°μ²΄ μ°Έμ‘°λ₯Ό 보κ΄νλ€.
/**
* μμμ± context μ proxy κ°μ²΄λ₯Ό λ¨Όμ ν λΉ λ°κ³ κ·Έ νμ
* find λ©μλλ₯Ό ν΅ν΄ νΈμΆ μ νμ
λΉκ΅
* ( em.find μμ μ μ€μ κ°μ²΄κ° μ‘°νκ° λλ€ )
*/
Member refMember = em.getReference(Member.class, member.getId());
System.out.println("Before refMember : "+refMember.getClass());
/**
* DB μ Entity μ‘°ν
*/
refMember.getName();
System.out.println("After refMember : "+refMember.getClass());
Before refMember : class org.example.entity.Member$HibernateProxy$8oxssjRD
Hibernate:
select
member0_.id as id1_0_0_,
member0_.createDate as createDa2_0_0_,
member0_.createUser as createUs3_0_0_,
member0_.modifyDate as modifyDa4_0_0_,
member0_.modifyUser as modifyUs5_0_0_,
member0_.name as name6_0_0_,
member0_.TEAM_ID as TEAM_ID7_0_0_,
team1_.id as id1_1_1_,
team1_.createDate as createDa2_1_1_,
team1_.createUser as createUs3_1_1_,
team1_.modifyDate as modifyDa4_1_1_,
team1_.modifyUser as modifyUs5_1_1_,
team1_.name as name6_1_1_
from
Member member0_
left outer join
Team team1_
on member0_.TEAM_ID=team1_.id
where
member0_.id=?
After refMember : class org.example.entity.Member$HibernateProxy$8oxssjRD
refMember
λ‘ Proxy κ°μ²΄λ₯Ό μμ±ν
refMember.getName();
λ₯Ό ν΅ν΄ Proxy κ°μ²΄ μ΄κΈ°νλ₯Ό μ§ννμμλ λμΌνκ² PROXY κ°μ²΄μ΄λ€
Member m1 = em.find(Member.class, member.getId());
System.out.println("m1 = "+m1.getClass());
Member reference = em.getReference(Member.class, member.getId());
System.out.println("reference = "+reference.getClass());
Hibernate:
select
member0_.id as id1_0_0_,
member0_.createDate as createDa2_0_0_,
member0_.createUser as createUs3_0_0_,
member0_.modifyDate as modifyDa4_0_0_,
member0_.modifyUser as modifyUs5_0_0_,
member0_.name as name6_0_0_,
member0_.TEAM_ID as TEAM_ID7_0_0_,
team1_.id as id1_1_1_,
team1_.createDate as createDa2_1_1_,
team1_.createUser as createUs3_1_1_,
team1_.modifyDate as modifyDa4_1_1_,
team1_.modifyUser as modifyUs5_1_1_,
team1_.name as name6_1_1_
from
Member member0_
left outer join
Team team1_
on member0_.TEAM_ID=team1_.id
where
member0_.id=?
m1 = class org.example.entity.Member
reference = class org.example.entity.Member
getReference λ₯Ό ν΅ν΄ Proxy κ°μ²΄λ₯Ό νΈμΆνμ§λ§ Entity κ°μ²΄ νμ μ΄ λ°ν λμλ€.
m1 = class org.example.entity.Member
reference = class org.example.entity.Member
1. Proxy κ°μ²΄κ° μ°Ύλ κ°μ²΄κ°Persistence Context ( 1μ°¨ Cache ) μ μ΄λ―Έ μ‘΄μ¬
νλ©΄
Proxy κ°μ²΄κ° ν΄λΉ κ°μ²΄ μ°Έμ‘°κ°μ κ°μ§ νμκ°μλ€.
2. JPA λ λμΌνTransaction μ λμΌν 쑰건μΌλ‘ μ‘°νλλ κ°λ€μ μ λΆ λμΌ
ν΄μΌνκΈ° λλ¬Έμ
κ°μ²΄ νμ μ λ°ν
Member beforeDetach = em.getReference(Member.class, member.getId());
System.out.println("reference : "+reference.getClass());
em.detach(beforeDetach);
beforeDetach.getName();
*reference : class org.example.entity.Member$HibernateProxy$WC3xU0Il
* org.hibernate.LazyInitializationException: could not initialize proxy [org.example.entity.Member#2] - no Session
* at org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:169)
* at org.hibernate.proxy.AbstractLazyInitializer.getImplementation(AbstractLazyInitializer.java:309)
* at org.hibernate.proxy.pojo.bytebuddy.ByteBuddyInterceptor.intercept(ByteBuddyInterceptor.java:45)
* at org.hibernate.proxy.ProxyConfiguration$InterceptorDispatcher.intercept(ProxyConfiguration.java:95)
* at org.example.entity.Member$HibernateProxy$WC3xU0Il.getName(Unknown Source)
* at org.example.ProxyDetachPersistence.main(ProxyDetachPersistence.java:52)
* 1μ 03, 2024 3:05:26 μ€ν org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl stop
* INFO: HHH10001008: Cleaning up connection pool [jdbc:h2:tcp://localhost/~/test;MODE=MySQL]
em.detach(beforeDetach);
μ€μμν μ΄νbeforeDetach.getName();
λ₯Ό ν΅ν΄
Proxy κ°μ²΄λ₯Ό μ΄κΈ°ν νλ©΄LazyInitializationException
μμΈκ° λ°μνλ€.
μ΄λ Proxy κ°μ²΄μ μ΄κΈ°νκ° μμμ± Context λ₯Ό ν΅ν΄ μ΄λ£¨μ΄μ§λλ°μ€μμνλ‘ μΈν κ°μ²΄ Session μ’ λ£
λλ¬Έμ΄λ€.
/**
*:::::::::::::::::::::::::::::::
* find ,reference !
* :::::::::::::::::::::::::::::::
*/
em.flush();
em.clear();
/**
* find λ‘ μ‘°ν
*/
Member mr1 = em.find(Member.class, member.getId());
Member mr2 = em.getReference(Member.class, member1.getId());
/**
* m1 , m2 νμ
λΉκ΅
*/
System.out.println("m1 : "+mr1.getClass());
System.out.println("m2 : "+mr2.getClass());
System.out.println("m1 == m2 : "+(mr1.getClass() == mr2.getClass()));
/**
*:::::::::::::::::::::::::::::::
* find ,reference Success !
* :::::::::::::::::::::::::::::::
*/
em.flush();
em.clear();
/**
* find λ‘ μ‘°ν
*/
Member mrs1 = em.find(Member.class, member.getId());
Member mrs2 = em.getReference(Member.class, member1.getId());
/**
* m1 , m2 νμ
λΉκ΅
* μ€μ λ‘λ μ΄λ λ‘μ§μΌλ‘ μ¬μ§ λͺ¨λ₯΄κΈ° λλ¬Έμ νμ
λΉκ΅ν λ
* == 보λ€λ instance of λ‘ μ¬μ©νλ κ²μ΄ μ’λ€.
*/
System.out.println("mrs1 : "+mrs1.getClass());
System.out.println("mrs2 : "+mrs2.getClass());
System.out.println("mrs1 : "+(mrs1 instanceof Member));
System.out.println("mrs2 : "+(mrs2 instanceof Member));
m1 : class org.example.entity.Member
m2 : class org.example.entity.Member$HibernateProxy$rOQSv1a1
m1 == m2 : false
Hibernate:
select
member0_.id as id1_0_0_,
member0_.createDate as createDa2_0_0_,
member0_.createUser as createUs3_0_0_,
member0_.modifyDate as modifyDa4_0_0_,
member0_.modifyUser as modifyUs5_0_0_,
member0_.name as name6_0_0_,
member0_.TEAM_ID as TEAM_ID7_0_0_,
team1_.id as id1_1_1_,
team1_.createDate as createDa2_1_1_,
team1_.createUser as createUs3_1_1_,
team1_.modifyDate as modifyDa4_1_1_,
team1_.modifyUser as modifyUs5_1_1_,
team1_.name as name6_1_1_
from
Member member0_
left outer join
Team team1_
on member0_.TEAM_ID=team1_.id
where
member0_.id=?
mrs1 : class org.example.entity.Member
mrs2 : class org.example.entity.Member$HibernateProxy$rOQSv1a1
mrs1 : true
mrs2 : true
equal
μ΄λ==
λ κ°μ²΄μ λ΄μ©μ΄λ μ°Έμ‘°κ° μμ ν λμΌν΄μΌtrue
λ₯Ό λ°ννλ€.
νμ§λ§ μ€μ logic μμ Proxy μ μ€μ Entity κ°μ²΄λ₯Ό ꡬλΆνμ§ μκ³ μ¬μ©νκΈ° λλ¬Έμ κ°μ²΄ νμ μ νμΈνλ €λ©΄instance of
λ₯Ό μ¬μ©νλ κ²μ΄ μ’λ€.