什么是Hibernate中的延迟加载? [英] What is lazy loading in Hibernate?

查看:113
本文介绍了什么是Hibernate中的延迟加载?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是Java中的延迟加载?我不明白这个过程。任何人都可以帮助我理解延迟加载的过程吗?

解决方案

如果您有父母,并且父母有一组子女。 Hibernate现在可以延迟加载子项,这意味着它在加载父项时并不实际加载所有的子项。相反,它会在请求时加载它们。你可以明确地请求这个,或者这更常见,当你试图访问一个子进程时,hibernate会自动加载它们。



延迟加载可以帮助改进性能显着,因为通常你不需要孩子,所以他们不会被加载。



还要小心n + 1问题。当你访问集合时,Hibernate实际上不会加载所有的孩子。相反,它会逐个加载每个孩子。在迭代集合时,这将导致每个孩子的查询。为了避免这种情况,你可以诱骗休眠同时加载所有的孩子,例如通过调用parent.getChildren()。size()。


What is lazy loading in Java? I don't understand the process. Can anybody help me to understand the process of lazy loading?

解决方案

Say you have a parent and that parent has a collection of children. Hibernate now can "lazy-load" the children, which means that it does not actually load all the children when loading the parent. Instead, it loads them when requested to do so. You can either request this explicitly or, and this is far more common, hibernate will load them automatically when you try to access a child.

Lazy-loading can help improve the performance significantly since often you won't need the children and so they will not be loaded.

Also beware of the n+1-problem. Hibernate will not actually load all children when you access the collection. Instead, it will load each child individually. When iterating over the collection, this causes a query for every child. In order to avoid this, you can trick hibernate into loading all children simultaneously, e.g. by calling parent.getChildren().size().

这篇关于什么是Hibernate中的延迟加载?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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