如何阻止Hibernate急切地获取多对一的关联对象 [英] How to stop Hibernate from eagerly fetching many-to-one associated object

查看:69
本文介绍了如何阻止Hibernate急切地获取多对一的关联对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个在多对一元素中声明的域对象的属性。

 <多对一名称=propertyNameclass =propertyClass fetch =selectnot-found =ignorelazy =proxy/> 

现在,这个想法是让Hibernate不急于获取这个属性。它可能是空的,所以设置了未找到的忽略。



但是,Hibernate在加载包含该关联的类时会自动加载实际的类(甚至没有代理)实例,当父类加载。由于某些属性的大小超过1MB,它们占用了大量的堆空间。



但是,如果找不到异常(或默认为异常),有这个属性的父类会加载代理!



如何阻止hibernate不加载代理,同时仍然允许此属性为null?



我发现lazy = no-proxy,但是文档讨论了某种字节码修改,并没有涉及任何细节。有人可以帮助我吗?

如果它很重要,它是Hibernate的Java版本,它至少是版本3(如果它可以查找实际版本但是现在是Hibernate 3+)。

我之前没有指定,但是Java版本是1.4。因此,不支持Java注释。

解决方案

如果关联的另一端可以是 null ,我相信Hibernate必须查询关联结束以确定它是否应该使用代理(如果另一端是null,则不需要代理)。我现在找不到这个引用,但我记得在某处读过它。



为了提供字段的延迟加载该文档涉及构建时字段代码的增强:使用lazy属性抓取。这里是一段摘录:


Hibernate3支持单个属性的延迟获取
。这个
优化技术也被称为
作为提取组。请注意,这个
主要是一种营销功能,如在
的练习中,优化行读取比优化
列读取更重要的多
。但是,在极端情况下,只有加载
类的某些属性可能是
,当旧式
表有数百列和
时,数据模型无法改进。 p>

惰性属性加载需要
buildtime字节码检测!如果
你的持久化类不是
增强的,Hibernate会默默地
忽略懒惰属性设置并将
降回到即时取回。



I have a property on a domain object that is declared in a many-to-one element. The basic syntax of this property looks like this:

<many-to-one name="propertyName" class="propertyClass" fetch="select" not-found="ignore" lazy="proxy" />

Now, the idea is to have Hibernate NOT eagerly fetch this property. It may be null, so the not-found ignore is set.

But, Hibernate, upon loading the class containing this association, takes it upon itself to load the actual class (not even a proxy) instance when the parent class is loaded. Since some properties are over 1MB in size, they take up a lot of the heap space.

If, however, not-found is set to exception (or defaulted to exception), the parent classes which have this property do load a proxy!

How can I stop hibernate from not loading a proxy, while still allowing this property to be null?

I found lazy=no-proxy, but the documentation talks about some sort of bytecode modification and doesn't go into any details. Can someone help me out?

If it matters, it is the Java version of Hibernate, and it is at least version 3 (I can look up the actual version if it helps, but it is Hibernate 3+ for now).

I didn't specify earlier, but the Java version is 1.4. So, Java annotations aren't supported.

解决方案

If the other end of the association can be null, I believe hibernate must query for the association end in order to determine if it should use a proxy or not (no need for proxy if the other end is null). I can't find the reference to this right now, but I remember reading it somewhere.

In order to provide lazy-loading of fields the documentation refers to bytecode enhancements on fields at buildtime: Using lazy property fetching. Here is an excerpt:

Hibernate3 supports the lazy fetching of individual properties. This optimization technique is also known as fetch groups. Please note that this is mostly a marketing feature, as in practice, optimizing row reads is much more important than optimization of column reads. However, only loading some properties of a class might be useful in extreme cases, when legacy tables have hundreds of columns and the data model can not be improved.

Lazy property loading requires buildtime bytecode instrumentation! If your persistent classes are not enhanced, Hibernate will silently ignore lazy property settings and fall back to immediate fetching.

这篇关于如何阻止Hibernate急切地获取多对一的关联对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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