在Hibernate中禁用延迟加载

在Hibernate中禁用延迟加载

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

问题描述

如何禁用Hibernate中的延迟加载?我正在使用持久性注释,而不是hbm xml文件。

我通过ID获取单个对象,并希望加载所有属性。在使用该对象之前,会话已关闭。



谢谢!

解决方案

您需要注释您希望非延迟加载的属性,并使用FetchType.EAGER

  @ManyToOne(fetch = FetchType。 EAGER)

您会发现,它不是您加载的对象,而是延迟加载的对象。相反,该对象的关联是懒惰的,如果这是你想要的行为,你需要告诉他们不要这样做。



如果这些对象也有想要加载的关联,你也需要注释它们。


How do I disable lazy loading in Hibernate? I am using persistence annotations, not an hbm xml file.

I am fetching a single object by ID and want all properties loaded. The session is closed before I use the object.

Thanks!

解决方案

You need to annotate the properties that you want non-lazy loaded with FetchType.EAGER

   @ManyToOne(fetch = FetchType.EAGER)

You see, it isn't the object that you are loading that is lazy loaded. Rather, that object's associations are lazy, and you need to tell them not to be if that is your desired behavior.

If those objects also have associations that you want loaded eagerly, you need to annotate them as well.

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

07-29 14:27