如何以编程方式关闭休眠的急切获取?

发布于 2024-08-04 13:08:11 字数 91 浏览 4 评论 0原文

我在映射中关联到急切加载的集合(lazy =“ false” fetch =“ subselect”)。当我执行查询时,如何使用 Hibernate 以编程方式关闭它?

I have in my mapping an association to an eagerly loaded collection (lazy="false" fetch="subselect"). How can I turn that off programmatically with Hibernate when I do a query?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

溺深海 2024-08-11 13:08:12

我遇到过一种情况,由于历史原因,在几个一对多依赖关系之间进行了急切的获取。多年来,许多地方开始依赖它,因此很难关闭它。
然而,在某些情况下,急切的获取会产生阻碍:对于表上的每个较大的选择,它都会为每个对象的每个集合生成数百个小子查询。
我找到了一种方法来解决这个问题,并没有真正覆盖急切的获取,但对我来说同样有用:只需创建一个一次性执行所有子获取的查询。这将对数据库进行 1 次物理查询,而不是让 hibernate 遍历依赖关系图并生成 100 个查询。

所以我替换

Query q = session.createQuery("from Customer c");

Query q = session.createQuery("from Customer c " +
            "left join fetch c.vats v " +
            "left join fetch v.klMemos bk " +
            "left join fetch bk.ferryKlMemos");

1 个客户有很多增值税号,1 个增值税号有很多 klmemos 等等。旧的情况将首先仅获取客户,然后休眠将开始逐个获取每个依赖集合。
第二种形式将在一个本机查询中加载所有内容,并且 hibernate 将找到填充对象缓存中的急切集合所需的所有内容。
希望它对某人有帮助。
注意:我仍然认为你应该尽量避免急切的获取;-)

I had a situation that for historical reasons did eager fetch between several one-to-many dependencies. Over years many places came to depend on it so it was hard to turn off.
However for some cases, the eager fetch was hindering: for every larger selection on the table, it would spawn 100s of small subqueries for each of the collections of each of the objects.
I found a way to get around this, not really overriding the eager fetch, but for me just as useful: simply create a single query that does all the subfetches at once. This will make 1 physical query to the database, instead of having hibernate walk the dependency graph and spawn 100s of queries.

So I replaced

Query q = session.createQuery("from Customer c");

by

Query q = session.createQuery("from Customer c " +
            "left join fetch c.vats v " +
            "left join fetch v.klMemos bk " +
            "left join fetch bk.ferryKlMemos");

1 Customer has many VAT numbers, 1 VAT number has many klmemos and so on. The old situation would first fetch only the customers and hibernate would then start fetching each of the dependent collections one by one.
The second form will load everything in one native query, and hibernate will find all it needs to populate the eager collections in the object cache.
Hope it helps somebody.
Note: I still think you should try to avoid eager fetches ;-)

浴红衣 2024-08-11 13:08:11

事实上,情况应该是相反的。您可以在映射中将其关闭,并在查询中使用“fetch”在特定用例上激活它。

这就是 Hibernate 团队的看法。 Hibernate 中无法创建一个为属性指定“no-fetch”的请求...

In fact, it is supposed to be the other way around. You turn it off in the mapping, and activate it on specific use cases with a "fetch" in the query.

That's the way the Hibernate team sees it. There is no way in Hibernate to create a request that specifies "no-fetch" for a property...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文