Hibernate:Hibernate是否总是使用对象代理? [英] Hibernate: Does Hibernate Always Use Object Proxies?

查看:95
本文介绍了Hibernate:Hibernate是否总是使用对象代理?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我认为只有当类有一个Collection类型的字段并使用Lazy fetching时才使用对象代理。但有些消息来源似乎表明,Hibernate 3使用代理来处理所有对象,而不管对象是否具有集合类型字段。



有人可以解释Hibernate何时使用对象代理?是一直以来,还是仅仅在某些情况下? 根据:$ b $ .html / hibernate / orm / 4.3 / manual / en-US / html / ch20.html#performance-fetching-lazyrel =nofollow noreferrer>


默认情况下,Hibernate使用集合的懒惰选择读取和单值关联的惰性代理读取。这些默认值对于大多数应用程序中的大多数关联都是有意义的。


所以如果你有一个标记为关联的对象一对一或多对一),那么它将是一个代理对象,直到您尝试访问它,此时Hibernate将尝试使用数据库中的值填充它。



AFAIK一个集合将被初始化为null,直到您尝试访问它,此时Hibernate将尝试为其提供水合物。



正如您在你的评论,是的,你的对象完全依赖于代理对象来请求它们时加载值。



当然,如果使用 fetchType.EAGER 关联。如果您是Hibernate的新手,我建议您阅读我编写的本指南。它涵盖了获取类型和配置不同类型关系等内容。


I thought that object proxies are used only when the class has a field of Collection type, and uses Lazy fetching. But some sources seem to suggest that Hibernate 3 uses proxies for all objects regardless of whether the object has field of collection type or not.

Can someone please explain when Hibernate uses object proxies? Is it all the time, or just in some cases?

解决方案

As per the Hibernate docs :

By default, Hibernate uses lazy select fetching for collections and lazy proxy fetching for single-valued associations. These defaults make sense for most associations in the majority of applications.

So if you have a single object marked as an association (one-to-one or many-to-one) then it will be a proxy object until you try to access it, at which point Hibernate will attempt to populate it with values from the database.

AFAIK a collection will be initialized as null until you try to access it, at which point Hibernate will attempt to hydrate it with values.

As you suggest in your comment, yes, your object is entirely dependent on the proxy object to load the values when you request them.

None of this applies of course if you use fetchType.EAGER on the association. If you are new to Hibernate I suggest perusing this guide that I wrote. It covers things like fetch types and config for different types of relationships.

这篇关于Hibernate:Hibernate是否总是使用对象代理?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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