Search code examples
javahibernatejpapersistence

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?


Solution

  • 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.