Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does Hibernate.initialize do?

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

Hibernate.initialize(entity.lazyCollection)

Is there any sense in

Hibernate.initialize(entity)
like image 422
Mike Avatar asked Oct 29 '13 19:10

Mike


People also ask

What is use of Hibernate initialize?

next difference is that Hibernate. initialize generates and executes additional sql for fetching data. So you can use it after session is closed. When you use Eager fetch in entity it's always fetch that collections during finding data (under the connection session ) in database, but not after it.

What is lazy initialization exception in Hibernate?

Class LazyInitializationExceptionIndicates an attempt to access not-yet-fetched data outside of a session context. For example, when an uninitialized proxy or collection is accessed after the session was closed.

How does Hibernate proxy work?

By definition, a proxy is “a function authorized to act as the deputy or substitute for another”. This applies to Hibernate when we call Session. load() to create what is called an uninitialized proxy of our desired entity class. This subclass will be the one to be returned instead of querying the database directly.

Could not initialize proxy the owning session was closed Hibernate?

We have seen that this error mainly comes when you have closed the connection and trying to access the proxy object which is no fully initialized. Since Proxy object needs a connection, you can either reattach object to the session or carefully avoid writing such code, which access uninitialized Proxy object.


1 Answers

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

like image 161
Alan Hay Avatar answered Sep 28 '22 06:09

Alan Hay