What does Hibernate.initialize do?

陌路散爱 提交于 2019-11-30 17:36:21

I would say yes if the Entity has a lazily initialized field e.g. some large BLOB or CLOB data or a lazy one-to-one association. See 20.1.8. in the documentation for the former and 20.1.3 for the latter.

See also:

20.1.4. Initializing collections and proxies

I agree with Alan Hay, here is my experience, I've had this problem when running the JUNit tests, some of the lazy objects were not loading when trying to load the objects in another session. I had to call the Hibernate.initialize(Object) to load the lazy objects into the memory.

Hibernate in some cases returns proxy object like lazy collection or Session.load() etc. So if you have proxy object and want the real one you can manually initialize it.

Shortly Hibernate.initialize() creates another query to fetch object in persistence context. When object is loaded eagerly JPA makes only one query to fetch object. So another difference is number calls to server

In addition : 1. If object is null Hibernate.initialize() throws exception 2. Good source for how to initialize lazy associations http://www.thoughts-on-java.org/5-ways-to-initialize-lazy-relations-and-when-to-use-them/

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