What does Hibernate.initialize do?
Asked Answered
A

4

15

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

Hibernate.initialize(entity.lazyCollection)

Is there any sense in

Hibernate.initialize(entity)
Algicide answered 29/10, 2013 at 19:1 Comment(0)
B
5

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

Byproduct answered 29/10, 2013 at 19:37 Comment(0)
P
1

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.

Plagal answered 29/10, 2013 at 21:19 Comment(0)
K
0

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.

Katydid answered 29/10, 2014 at 4:32 Comment(0)
A
0

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/

Alberta answered 13/12, 2016 at 9:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.