如何在运行时重写FetchType.EAGER以延迟 [英] How to override FetchType.EAGER to be lazy at runtime

查看:171
本文介绍了如何在运行时重写FetchType.EAGER以延迟的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用JPA EntityManager和JPA Query对象,我该如何重写一些注解@OneToMany(fetch = FetchType.EAGER)以便在查询中被延迟取出?



如果我有hibernate Query对象,我可以让它创建一个标准对象并使用它,将获取类型设置为懒惰。但是我必须使用JPA查询对象。有没有解决这个问题的方法?即使使用原生的Hibernate API,也无法做到这一点。解析方案

如果一个关联被定义为EAGER,它总是被急切地加载,并且没有办法改变使用查询的方式。



反过来是不正确的:您可以使用查询来加载惰性关联。


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?

解决方案

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.

这篇关于如何在运行时重写FetchType.EAGER以延迟的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆