Sanjay Kumar
Sanjay Kumar

Reputation: 331

In an object graph fetch N level objects using eager loading and then lazy loading

Suppose we have an object graph as below:

Class A {
  B b;
}
Class B {
  C c;
}
Class C {
  D d;
}
Class D {}

Now when I fetch the object A I want to fetch B and C using eager loading and D using lazy loading. How this can be done in JPA or Hibernate?

Upvotes: 0

Views: 73

Answers (2)

Youcef LAIDANI
Youcef LAIDANI

Reputation: 59978

Just use eager in A for Object B and eager in B for Object C and lazy in C for object D

class A {
  //eager
  B b;
}
class B {
  //eager
  C c;
}
class C {
  //lazy
  D d;
}
class D {}

Upvotes: 1

Eugene
Eugene

Reputation: 120858

Assuming there is a connection between them, you can easily use FetchType.EAGER in B and C and FetchType.LAZY in D.

This sort of can be achieved via @NamedEntityGraph also.

Upvotes: 2

Related Questions