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

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

问题描述

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

I know to use lazily load objects/collections outside the session, we do Hibernate.initialize(Object obj) so that object that is passed as an 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.

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

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 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.

如果你平时懒加载一个集合,但是为了特定的用途,你需要确保在会话关闭之前已经加载了集合,你可以使用Hibernate.initialize(Object obj) 正如你所指出的.

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天全站免登陆