Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to override FetchType.EAGER to be lazy at runtime

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?

like image 844
hugelgupf Avatar asked Jun 12 '12 13:06

hugelgupf


1 Answers

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.

like image 164
JB Nizet Avatar answered Oct 10 '22 15:10

JB Nizet