What does Hibernate.initialize do?

送分小仙女□ 提交于 2019-11-30 01:35:29

问题


What does Hibernate.initialize do?
Usually referred documentation talk only about

Hibernate.initialize(entity.lazyCollection)

Is there any sense in

Hibernate.initialize(entity)

回答1:


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




回答2:


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.




回答3:


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.




回答4:


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/



来源:https://stackoverflow.com/questions/19666928/what-does-hibernate-initialize-do

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