Search code examples
javahibernateone-to-manyhibernate-onetomany

Is it possible for Hibernate to fetch two levels of sets with one select?


I have three entities:

@Entity
@Table(name="a")
class A {

  @Id
  Long id;

  @OneToMany(fetch = FetchType.EAGER)
  @JoinColumn(name = "a_id")    
  Set<B> bs;

  // ... other fields
}

@Entity
@Table(name = "b")
class B {

  @Id
  Long id;

  @OneToMany(fetch = FetchType.EAGER)
  @JoinColumn(name = "c_id")
  Set<C> cs;

  // ... other fields
}

@Entity
@Table(name = "c")
class C {

  @Id
  Long id;

  // ... other fields
}

When I use Criteria API from JPA to get given A, Hibernate first fetches just A, then for each A, all B's and finally for each B all C's.

Is it possible to force fetching with one select which is in theory possible?


Solution

  • Hibernate doesn't support well fetching multiple eager collections. Take a look here.

    Btw do you really need those collection as eagerly loaded?

    Hibernate cannot simultaneously fetch multiple bags