java - Does hibernate load two seprate copies of same instance if they are loaded twice from database? -
i know there many different question on regarding lazy load mine 1 bit different.
lets have entity in have collection of entity b. similarly, in entity b, have collection of a. in both case, lazy="true" option used.
entity a's instace aa has -->set<b>
===(this set contains entity b's instance bb)
entity b's instace bb has -->set<a>
===(this set contains entity a's instance aa)
now if load entity a's collection(which set<b>
).its initialized now, i.e. complete a's aa instance including collection. expect entity b's instance bb initialized no, not , lazy initialization exception when refer entity b's collection has entity a's instance aa.
does hibernate load 2 seprate copies of same instance if loaded twice database? if so, there way synchronize changes in copies within session?
hope clear enough , haven't messed things messy info:)
the flow this:
- you load
aa
,ab
. collections lazy , proxied. - you initialize
aa
's collection. among others, contains instanceab
, same 1 loaded before (the same java object). collection inab
instance is still uninitialized; didn't access it, hibernate doesn't waste time initialize it. @ time, unknown in sessionaa
member ofab
's collection. - you access (initialize)
ab
's collection. among others, contains instanceaa
, same 1 loaded before (the same java object).
as can see, there no duplicates in current session (persistence context). there may different proxies/collections, delegate to/contain same entity instance.
Comments
Post a Comment