Hibernate.initialize()如何工作 [英] How Hibernate.initialize() works

查看:91
本文介绍了Hibernate.initialize()如何工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道在会话之外使用延迟加载对象/集合,我们做 Hibernate.initialize(Object obj),以便作为参数传递给initialize()方法已初始化,可用于会话范围之外。



但我无法理解这是如何工作的。我的意思是,如果我们这样做了,那么我们最终会提前进行提取,所以为什么我们在配置中做了懒惰,并最终在运行时提前获取。



换句话说,我想知道使用 Hibernate.initialize() eagerly 加载该对象的区别。



我是否弄错了或者错过了什么?



为了避免收集关联而加载收集关联的原因是为了避免每次加载父对象时加载收集,如果您不需要的话。



如果您正常延迟加载集合,但对于特定用途,您需要确保在会话关闭之前加载集合,可以使用 Hibernate.initialize(Object obj)如您所述。

需要加载的集合,你应该确实加载它gerly。但在大多数软件中,情况并非如此。


I know to use lazily load objects/colletions outside the session, we do Hibernate.initialize(Object obj) so that object that is passed as argument to initialize() method is initialized and can be used outside of the scope of the session .

But what I am not able to understand how this works . I mean if we are doing then we end up in having eager fetching so why we did lazy in the configuration and end up in the eager fetching while runtime.

In other words, i want to know the difference between using Hibernate.initialize() and eagerly loading that object.

Did i get it wrong or miss something?

解决方案

The difference is in scope of application.

The reason for making a collection association lazy is to avoid having it load the collection every time the parent object is loaded if you don't really need it.

If you are lazy-loading a collection normally, but for a particular use, you need to ensure the collection has been loaded before the session is closed, you can use Hibernate.initialize(Object obj) as you noted.

If you in fact always need the collection loaded, you should indeed load it eagerly. In most software though, that isn't the case.

这篇关于Hibernate.initialize()如何工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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