12

Using the JPA EntityManager and the JPA Query object, how can I override something that has the annotation @OneToMany(fetch = FetchType.EAGER) to be fetched lazily in a query?

If I had the hibernate Query object, I could have it create a criteria object and using this, set the fetch type to be lazy. But I have to use the JPA Query object. Is there any solution for this problem?

hugelgupf
  • 277
  • 5
  • 13

2 Answers2

8

There is no way to do that, even with the native Hibernate API. If an association is defined as EAGER, it will always be eagerly loaded, and there's no way to change that using a query.

The reverse is not true: you can eagerly-load a lazy association using a query.

JB Nizet
  • 633,450
  • 80
  • 1,108
  • 1,174
-1

Look into Hibernate Fetch profiles, or JPA Entity Graphs. This problem has been solved since you've asked the question in 2012.

mancini0
  • 3,075
  • 21
  • 25
  • 1
    This has not been solved see: https://hibernate.atlassian.net/browse/HHH-8776 - it is not possible to to ignore the EAGER load even using EntityGraphs. – Robert Hunt Oct 19 '17 at 09:53
  • Ahh my apologies, disregard my answer. The typical use case is the reverse - mark your entities as lazily fetched, and use the entity graph to change the fetch type to be eager at run time. I was not aware of HHH-8776, good to know, thanks. – mancini0 Jan 29 '18 at 17:40