Hibernate: Does Hibernate Always Use Object Proxies?

久未见 提交于 2019-12-02 07:14:15
JamesENL

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.

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