Hibernate bidirectional query
I have a question regarding Hibernate bidirectional. Lets say I have 2 classes, Class A
and Class B
and bidirectionally related. That means when I query Class A
, I also can get Class B
and vice versa. My question is how hibernate work when I do this
Clas开发者_StackOverflow中文版sA classA = ClassA.findClassAById(1);
ClassB = classA.getClassB().getClassA().getClassB();
I know I can get ClassB
with just classA.getClassB();
, but I also can get classA.getClassB().getClassA().getClassB().getClassA().getClassB()...;
Can someone explain to me how hibernate work with bidirectional query?
I'm concerned about performance.
Hibernate employes an efficient first level cache also known as the PersistantContext If an object is loaded in the context , hibernate does not hit the database to get the same object.
In the problem statement when findClassAById(1)
is called classA
object is loaded in the persistance context. classB
object is loaded at the same time or later depending on the lazy loading/eager loading strategy adopted. Following which the database should not be hit ideally for all future calls.
You can very well google Persistance Context for more information about how it works.
精彩评论