How to override FetchType.EAGER to be lazy at runtime

谁说我不能喝 提交于 2019-11-30 03:01:43

问题


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?


回答1:


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.




回答2:


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



来源:https://stackoverflow.com/questions/10997321/how-to-override-fetchtype-eager-to-be-lazy-at-runtime

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!