2

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?

Patryk Dobrowolski
  • 1,565
  • 2
  • 13
  • 29

2 Answers2

2

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

Community
  • 1
  • 1
Łukasz Rzeszotarski
  • 5,791
  • 6
  • 37
  • 68
  • It doesn't really matter what kind of fetching I choose. It's instantly converted into another object graph to keep in memory. – Patryk Dobrowolski Mar 19 '13 at 09:41
  • I tried another approach and fetch using C as a root. But seems it doesn't work any faster. Probably because fetching is costly itself, and that's because of CLOB fields here, I bet. I'll leave it as it is and change fetching to LAZY. Need to live with this. – Patryk Dobrowolski Mar 19 '13 at 09:44
0

Sounds like not possible without internal iteration.

In such cases i would like to suggest Hibernate Native Sql query.

Suresh Atta
  • 120,458
  • 37
  • 198
  • 307