Hibernate: Does Hibernate Always Use Object Proxies?

匿名 (未验证) 提交于 2019-12-03 09:05:37

问题:

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?

回答1:

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.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!